Silverlight Asynchronous Wait Method by Synchronization Method

I have a strange problem. I want the wait for an asynchronous call to be executed when the synchronization function is called. In other projects, I have successfully used ResetEvents, but in sl it doesn't seem to work.

    //Sync call save some value in storage
    public static void SaveValue(string key, object value, bool encrypted)
    {
        if (encrypted)
        {
            isEncrypting = true;
            var Registrator = new RegistratorClient();
            Registrator.EncryptCompleted +=Registrator_EncryptCompleted;
            Registrator.EncryptAsync(obj); //async call
            while (isEncrypting)
                 Thread.Sleep(10); 
            return;
        }
        ...
    }
    static void Registrator_EncryptCompleted(object sender, EncryptCompletedEventArgs e)
    {
        if (String.IsNullOrEmpty(fieldToSave))
            return;
        App Appvars = Application.Current as App;
        if (Appvars.Settings.Contains(fieldToSave))
            Appvars.Settings[fieldToSave] = e.Result;
        else
            Appvars.Settings.Add(fieldToSave, e.Result);
        isEncrypting = false;
    }

This method also does not work :( Please help. What happened?

+1
source share
2 answers

- , SL . , , , , .

- SL. , Caliburn. , async.

+2

: -

* =

:

async * sync = async.

async * sync = sync, , . , . SaveValue , , .

, true , SaveValue.

: -

public static void SaveValue(string key, object value)
{
    App Appvars = Application.Current as App;
    if (Appvars.Settings.Contains(key))
        Appvars.Settings[key] = value;
    else
        Appvars.Settings.Add(key, value);
}

public static void SaveValue(string key, object value, Action doneCallback)
{

    var Registrator = new RegistratorClient();
    Registrator.EncryptCompleted += (s, args) =>
    {
         // Really should consider some exception handling here.
         SaveValue(key, s.Result);
         if (doneCallback != null)
             doneCallback();
    }
    Registrator.EncryptAsync(value); //async call
}

SaveValue , , , doneCallback . SaveValue .

0

Source: https://habr.com/ru/post/1757946/


All Articles