Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What thread calls the completed event handler on silverlight WCF calls?

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?

like image 799
Anders Abel Avatar asked Jun 07 '11 10:06

Anders Abel


2 Answers

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.

like image 105
Aardvark Avatar answered Oct 12 '22 09:10

Aardvark


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.

like image 45
AnthonyWJones Avatar answered Oct 12 '22 07:10

AnthonyWJones