【发布时间】:2016-07-14 19:57:18
【问题描述】:
Silverlight 项目。
在我后面的 View 代码中,我调用 View Model 中的一个方法来获取值。
public MyViewModel ViewModel
{
get
{
if (this.viewModel == null)
this.viewModel = new MyViewModel();
return this.viewModel;
}
set
{
if (this.viewModel != value)
{
this.viewModel = value;
}
}
}
但是没有调用异步回调。有时它被延迟调用。因此,我得到的值是1900-01-01 00:00:00.000,而不是正确的日期时间。
DateTime value;
public void GetDateTime()
{
var proxy = new WcfClient();
proxy.GetCurrentDateTimeCompleted -= ProxyGetCurrentDateTimeCompleted;
proxy.GetCurrentDateTimeCompleted += ProxyGetCurrentDateTimeCompleted;
proxy.GetCurrentDateTimeAsync();
}
private void ProxyGetCurrentDateTimeCompleted(object sender, GetCurrentDateTimeCompletedEventArgs args)
{
try
{
value = args.Result;
在后面的代码中调用它。
this.viewModel.GetDateTime();
更新 1
根据评论,我添加了一些解释。视图有一个复选框。如果我单击它或取消单击它,将弹出一个带有是/否按钮的消息框。无论您选择是或否,我都会在MessageBoxYesNoSelection_Closed 事件中记录日期和时间。事件方法还在后面的代码中。
复选框部分代码为:
if (clickedCheckBox != null)
{
var msgBoxControl = new MessageBoxControl();
msgBoxControl.Closed -= MessageBoxYesNoSelection_Closed;
msgBoxControl.Closed += MessageBoxYesNoSelection_Closed;
if (clickedCheckBox.IsChecked == true)
{
var curDateTime = this.ViewModel.value;// 1900-01-01 00:00:00.000
在MessageBoxYesNoSelection_Closed 中,我们得到了值。
this.ViewModel.GetDateTime();// WCF async call
this.ViewModel.UpdateValue(value); // another WCF async call, need value from GetDateTime() method's return result.
this.ViewModel.UpdateAnotherValue(value, user); // third WCF async call
我发现有时虽然调用了异步回调,但它们并没有执行。
【问题讨论】:
-
您需要提供更多关于在异步功能完成后如何获取值
value的信息,您需要提供Minimal Complete Verifiable Example,现在您遗漏了太多信息我们可以有效地帮助您。 -
@ScottChamberlain,感谢您的评论。我添加了一些代码。
-
在您的服务类中设置断点并观察那里发生的情况。服务器是否收到请求?超时时间是否太短?对我来说很多猜测......
-
@lokusking,服务器确实收到了请求。问题是有时我能得到正确的值,有时不能。
-
Sometimes 表示在某些情况下存在超时问题。您的服务器方法是否被
lock包围?
标签: c# wcf asynchronous