什么叫线程Silverlight的WCF调用完成事件处理程序?什么叫、线程、事件、程序

2023-09-03 02:45:45 作者:安稳又干净

假设我有Silverlight应用程序做一个调用WCF服务:

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 被称为UI线程。最终会叫什么线程 DoStuffCompleted 的方法?如果我在调用同时与两个异步调用,是有可能,无论是完成的事件同时发射,在不同的线程?

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?

推荐答案

回调将在主线程中调用。多重反应不会同时出现。响应事件的顺序可能是意想不到的。您可能需要使用proxy.DoStuffAsync的重载接受用户状态的对象:

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(对象userState)

这将让你送点东西唯一的每个调用这样你就可以区分哪些回应你处理。请记住,如果WCF调用返回没有返回值的错误 - 所以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).

更新:

找到一些更多的信息(在SO)就如何使其使用另一个线程:

Found some more info (on SO) on how to make it use another thread:

的Silverlight Web服务的回调性能按照链接那里的了Tomek 的的博客的其它更多信息

Silverlight web service callback performance Follow the link there to Tomek's blog for lots more info.