哪个线程在 Silverlight WCF 调用上调用已完成的事件处理程序? [英] What thread calls the completed event handler on silverlight WCF calls?

查看:25
本文介绍了哪个线程在 Silverlight WCF 调用上调用已完成的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 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(object 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.

这篇关于哪个线程在 Silverlight WCF 调用上调用已完成的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆