哪个线程进行异步回调运行? [英] On which thread do Async Callbacks run?

查看:138
本文介绍了哪个线程进行异步回调运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做几个 HttpWebRequest.BeginGetResponse 呼叫,并在该 BeginGetResponse ,我的回调方法中号调用的事件处理程序。在事件处理程序,有逻辑测试是否下载成功。如果不是,它会尝试重新下载的HTML。我注意到很多的线程正在生成尤其是当有错误。因此,其上线做了异步回调运行?

I'm making several HttpWebRequest.BeginGetResponse calls, and in the callback method of the BeginGetResponse, I'm invoking an EventHandler. In the EventHandler, there is logic to test if the download was successful. If not, it tries to redownload the Html. I'm noticing lots of threads being generated especially when there are errors. So, on which thread do the Async Callbacks run?

反正我有可以调用原始线程的事件处理程序?如果不是更多钞票,我可以调用它的UI线程?

Is there anyway I can invoke the EventHandler on the original thread? If that is not posible, can I invoke it on the UI thread?

谢谢!

推荐答案

回调在一个线程池的线。目前在.NET中没有机制来利用特定线程code运行。这是很难得的,你不能只中断一个线程,而它正忙,使其运行一些code。这会导致可怕的重入问题,锁也解决不了。

Callbacks are made on a threadpool thread. There is no mechanism in .NET to make code run on a specific thread. That is very hard to come by, you can't just interrupt a thread while it is busy and make it run some code. That causes horrible re-entrancy problems that a lock cannot solve.

一个线程必须处于空闲状态,不积极变异程序的状态。有一种线程,其行为的方式,在一个WinForms或WPF应用程序的UI线程。这也是有对付那些根本线程安全的,任何有关的UI对象的线程。这不是巧合。

A thread must be in an idle state, not actively mutating the state of the program. There's one kind of thread that behaves that way, the UI thread in a Winforms or WPF app. That's also the thread that has to deal with objects that are fundamentally thread-unsafe, anything related to the UI. This is not a coincidence.

这两个类库使人们有可能从元帅一个工作线程UI线程调用,专门帮助获得一个线程安全的方式更新UI。在你的WinForms使用Control.Begin / Invoke的(),在WPF使用Dispatcher.Begin / Invoke的()。 BackgroundWorker的是一个方便的类来获得这个没有明确管理编组完成。但不适合I / O完成回调。

Both class libraries make it possible to marshal a call from a worker thread to the UI thread, specifically to help getting the UI updated in a thread-safe way. In Winforms you use Control.Begin/Invoke(), in WPF you use Dispatcher.Begin/Invoke(). BackgroundWorker is a handy class to get this done without explicitly managing the marshaling. But isn't suitable for I/O completion callbacks.

这篇关于哪个线程进行异步回调运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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