哪个线程(S)不WebClient的提高它的事件? [英] On which thread(s) does WebClient raise its events?

查看:101
本文介绍了哪个线程(S)不WebClient的提高它的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到它指定线程的WebClient提高其事件的任何文档。我跑了一些测试,并确定以下内容:

I can not find any documentation that specifies on which thread WebClient raises its events. I ran some tests and determined the following:


  • 如果从UI线程调用(从事件处理程序说吧)事件处理程序将在该线程执行。作为测试,我添加了调用OpenReadAsync后一个无限循环。事件处理程序从未被调用。

  • If called from a UI thread (say from an event handler) the event handler will be executed on that thread. As a test, I added an infinite loop after the call to OpenReadAsync. The event handler was never called.

如果没有UI线程,像在控制台应用程序,该事件处理程序将在一个线程池线程来执行。在这种情况下,如果我想提供一些成果应用的其余部分,我就必须要知道的线程问题。

If there is no UI thread, like in a console application, the event handler will be executed on a thread pool thread. In this case, if I wanted to provide some results the rest of the application, I would have to be aware of threading issues.

时的这种行为记录任何地方?我什么也没找到。

Is this behaviour documented anywhere? I found nothing.

我有关于C#的新异步功能基本相同的问题 - 最终,异步code将被执行。将还生成一个线程池线程时,有没有UI线程?请问,反过来,要求线程安全code?

I have the basically the same question concerning the new async features of C# - eventually, the asynchronous code will have to be executed. Will that also spawn a thread pool thread when there is no UI thread? Will that, in turn, require thread safe code?

我觉得我在这里失去了一些东西 - 我只能找到这个信息非常少,但这似乎对我很重要。

I feel that I am missing something here - I can only find very little information about this, but this seems important to me.

推荐答案

有关 Web客户端,我还没有找到它记录下去,但已经看到了相同的行为,你。本质上,这可谓是如果有,在通话开始的活动同步的背景下,它的使用 - 否则,使用线程池

For WebClient, I haven't found it documented either, but have seen the same behaviour as you. Essentially this can be described as "if there's an active synchronization context when the call is started, it's used - otherwise the thread pool is used."

有关在C#5异步行为,这取决于无论你等待的实现......但我相信的awaiter任务&LT; T&GT; 将使用<一href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.current.aspx\"><$c$c>TaskScheduler.Current安排的延续 - 这意味着你会看到同一类行为。 (这是不一定的只是的UI线程它设置一个任务调度程序,但是这是最明显的例子。)

For the async behaviour in C# 5, it depends on the implementation of whatever you're awaiting... but I believe the awaiter for Task<T> will use TaskScheduler.Current to schedule a continuation - which means you'll see the same sort of behaviour. (It's not necessarily just a UI thread which sets a task scheduler, but that's the most obvious example.)

在使用线程池中的线程,它应该仍然是线程安全 - 该方法仅在单个线程同时执行,我相信任务并行库执行所有需要的内存屏障

When thread pool threads are used, it should still be thread-safe - the method is only executing in a single thread at a time, and I believe the Task Parallel Library performs all the required memory barriers.

如果您有兴趣异步如何一起挂在幕后,你可能想读我的 Eduasync博客系列

If you're interested in how async hangs together behind the scenes, you might want to read my Eduasync blog series.

这篇关于哪个线程(S)不WebClient的提高它的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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