Silverlight InvokeAndBlockUntilCompletion of a callback

Aug 19, 2012 at 5:09 PM
Edited Aug 19, 2012 at 5:09 PM


I want to delay processing of my method until a callback comes e.g. I have written:

function GetResults(string property)
   UISynchronizationContext.Instance.InvokeAndBlockUntilCompletion(() => 
             Compute(property, delegate(object sender, GetValuesCompletedEventArgs args)
               if (args.Values != null)
   return results;

I want that the return results line is not executed until the callback delegate is invoked but its not working that way. Maybe, the problem is with the callback delegate, it is our own custom delegate, EventArgs extension and can be modified if required.

What can I do to make this process synchronous?


Aug 22, 2012 at 9:12 AM

Hi Akshayakrsh,

The first thing to be aware of is that Silverlight does not allow you to perform a blocking web service call from the UI thread. If you want to block during such a call, the call must be made from a different -non UI- thread. 

But, your code isn't going to work anyway since unless the Compute method completes synchronously, your callback delegate will be called at some other time.

To make the process synchronous you either need to perform the GetValues call on a background thread and block until it completes, or you might want to look at RX or the new async keyword.