Assume that I have Silverlight app doing a call to a WCF service:
void DoStuff()
{
MyProxy proxy = new MyProxy();
proxy.DoStuffCompleted += DoStuffCompleted;
proxy.DoStuffAsync();
}
void DoStuffCompleted(object sender, DoStuffCompletedEventArgs e)
{
// Handle the result.
}
DoStuff
is called by the UI thread. What thread will eventually call the DoStuffCompleted
method? If I invoke two async calls at the same time, is there a possibility that both the completed events are fired simultaneously, on different threads?
The callback will be invoked on the main thread. Multiple responses will not occur simultaneously. The order of the response events could be unexpected. You may want to use the overload of proxy.DoStuffAsync that accepts "user state" object:
proxy.DoStuffAsync(object userState)
This will allow you to send something unique for each call so you can differentiate which response you're dealing with. Remember that if the WCF call returns an error you have no return value - so userState may be the only way to know which call failed (if it matters).
Update:
Found some more info (on SO) on how to make it use another thread:
Silverlight web service callback performance Follow the link there to Tomek's blog for lots more info.
The Completed event will occur on a different thread than the UI Thread. Multiple Completed events may be executed simultaneously on different threads because a thread pool is used to handle results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With