简体   繁体   English

哪个线程调用 silverlight WCF 调用上的已完成事件处理程序?

[英]What thread calls the completed event handler on silverlight WCF calls?

Assume that I have Silverlight app doing a call to a WCF service:假设我有 Silverlight 应用程序正在调用 WCF 服务:

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. DoStuff由 UI 线程调用。 What thread will eventually call the DoStuffCompleted method?哪个线程最终会调用DoStuffCompleted方法? 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:您可能希望使用接受“用户状态”object 的 proxy.DoStuffAsync 的重载:

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).请记住,如果 WCF 调用返回错误,则您没有返回值 - 因此 userState 可能是知道哪个调用失败(如果重要)的唯一方法。

Update:更新:

Found some more info (on SO) on how to make it use another thread:找到了一些关于如何使它使用另一个线程的更多信息(关于 SO):

Silverlight web service callback performance Follow the link there to Tomek 's blog for lots more info. Silverlight web 服务回调性能按照链接到Tomek的博客了解更多信息。

The Completed event will occur on a different thread than the UI Thread. Completed 事件将发生在与 UI 线程不同的线程上。 Multiple Completed events may be executed simultaneously on different threads because a thread pool is used to handle results.多个 Completed 事件可以在不同的线程上同时执行,因为线程池用于处理结果。

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. DoStuffCompleted将在后台池线程中执行。

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.如果我们想用新获得的数据更新 UI(比如我们想更新数据网格控件以显示客户数据),我们必须小心在 UI 线程上执行此操作。 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 )如果我们不这样做,那么可能会发生各种奇怪的事情,我们将很难诊断要修复哪个错误(从这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM