I’ve got strange issue. I want wait end async call at sync function call. At other projects I successfully use ResetEvents, but in sl it don’t seem to be working.
//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 isn’t work too:( Help please. What’s wrong?
The problem comes from the fact that in SL all service calls are made on the UI thread. So you have one method trying to sleep on the UI thread, waiting on a callback that has to happen on the UI thread.
It is impossible to create a truly synchronous call in SL. If you really want to work with things in an synchronous fashion take a look at Caliburn. He uses some cool co-routine patterns to simulate synchronous programming while leaving the actual service calls async.