Asynchronous callback is called, but not executed sometimes

Silverlight project.

In my View code, I call a method in Model View to get the value.

 public MyViewModel ViewModel
    {
        get
        {
            if (this.viewModel == null)
                this.viewModel = new MyViewModel();
            return this.viewModel;
        }
        set
        {
            if (this.viewModel != value)
            {
                this.viewModel = value;
            }
        }
    }

However, an asynchronous callback is not called. Sometimes it is caused by a delay. This way I get the value 1900-01-01 00:00:00.000instead of the correct time.

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;

Call the code behind.

this.viewModel.GetDateTime();

Update 1

In the comment, I added a few explanations. The "View" box is checked. If I click on it or cancel it, a window with the Yes / No button will appear. Whatever you choose “Yes” or “No”, I will record the date and time in the event MessageBoxYesNoSelection_Closed. The event method is still in code.

Flag Code Code:

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

Inside MessageBoxYesNoSelection_Closedwe get the value.

this.ViewModel.GetDateTime();// WCF async call
this.ViewModel.UpdateValue(value); // another WCF async call, need value from GetDateTime() method return result.
this.ViewModel.UpdateAnotherValue(value, user); // third WCF async call

, , , .

+4
1

, , this.ViewModel.GetDateTime(), , (ProxyGetCurrentDateTimeCompleted) , (this.ViewModel.UpdateValue(value)).

- async await, .

, , , .

1

async await Silverlight 4 .NET Framework 4.0. . Microsoft.Bcl.Async. , , Visual Studio 2012, , Visual Studio, .

, , , . GetDateTime():

this.ViewModel.GetDateTime(); // WCF async call

:

private void ProxyGetCurrentDateTimeCompleted(object sender, GetCurrentDateTimeCompletedEventArgs args)
{
    value = args.Result;
    UpdateValue(value); // another WCF async call, need value from GetDateTime()
    UpdateAnotherValue(value, user); // third WCF async call
}

2

, , . . , , , . . :

DateTime value;
bool wcfCallInProgress;
public void GetDateTime()
{
    var proxy = new WcfClient();
    proxy.GetCurrentDateTimeCompleted -= ProxyGetCurrentDateTimeCompleted;
    proxy.GetCurrentDateTimeCompleted += ProxyGetCurrentDateTimeCompleted;
    wcfCallInProgress = true;
    proxy.GetCurrentDateTimeAsync();
}

private void ProxyGetCurrentDateTimeCompleted(object sender, GetCurrentDateTimeCompletedEventArgs args)
{
    value = args.Result;
    wcfCallInProgress = false;
}

, , :

this.ViewModel.GetDateTime();// WCF async call

while (this.ViewModel.wcfCallInProgress)
{
    Thread.Sleep(10);
}

this.ViewModel.UpdateValue(value); // another WCF async call, need value from GetDateTime() method return result.
this.ViewModel.UpdateAnotherValue(value, user); // third WCF async call

, , - . .

+3

All Articles