开发者

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

Assume that I have Silverlight app doing a call to a WCF service:

vo开发者_Python百科id 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.


Asynch calls are executed in the background thread pool. For each asynch call you shall have a separate thread from the pool.

DoStuffCompleted will be executed in the background pool thread.

Now, it is important to note that this method is called on the background worker thread. If we want to update the UI with the newly obtained data (say we want to update a data grid control to display the customer data), we have to be careful to do this on the UI thread. If we don't, then all manner of strange things may happen and we will have a difficult time diagnosing which bug to fix (from here)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜