简体   繁体   English

WebClient在哪个线程上引发事件?

[英]On which thread(s) does WebClient raise its events?

I can not find any documentation that specifies on which thread WebClient raises its events. 我找不到任何指定WebClient在哪个线程上引发事件的文档。 I ran some tests and determined the following: 我进行了一些测试,并确定了以下内容:

  • If called from a UI thread (say from an event handler) the event handler will be executed on that thread. 如果从UI线程(例如从事件处理程序)调用,则事件处理程序将在该线程上执行。 As a test, I added an infinite loop after the call to OpenReadAsync. 作为测试,我在调用OpenReadAsync之后添加了一个无限循环。 The event handler was never called. 从未调用过事件处理程序。

  • If there is no UI thread, like in a console application, the event handler will be executed on a thread pool thread. 如果没有UI线程(例如在控制台应用程序中),则事件处理程序将在线程池线程上执行。 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. 我什么都没找到。

I have the basically the same question concerning the new async features of C# - eventually, the asynchronous code will have to be executed. 关于C#的新异步功能,我有基本相同的问题-最终,必须执行异步代码。 Will that also spawn a thread pool thread when there is no UI thread? 当没有UI线程时,是否还会生成线程池线程? 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. 我觉得我在这里错过了一些东西-我只能找到很少的信息,但这对我来说似乎很重要。

For WebClient , I haven't found it documented either, but have seen the same behaviour as you. 对于WebClient ,我也没有找到任何文档,但是看到的行为与您相同。 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." 本质上,这可以描述为“如果调用开始时有活动的同步上下文,则使用它-否则使用线程池。”

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. 对于C#5中的异步行为,它取决于您正在等待的内容的实现...但是我相信Task<T>等待者将使用TaskScheduler.Current来安排继续时间-这意味着您将看到相同的内容某种行为。 (It's not necessarily just a UI thread which sets a task scheduler, but that's the most obvious example.) (不一定设置任务计划程序的UI线程,但这是最明显的示例。)

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. 使用线程池线程时,它仍应是线程安全的-该方法一次仅在单个线程中执行,我相信任务并行库会执行所有必需的内存屏障。

If you're interested in how async hangs together behind the scenes, you might want to read my Eduasync blog series . 如果您对异步如何在后台挂在一起感兴趣,您可能需要阅读我的Eduasync博客系列

The WebClient Class implements the Event-based Asynchronous Pattern . WebClient类实现基于事件的异步模式 The pattern is fully described in the Framework Design Guidelines, but MSDN also provides a few hints how it is implemented : 该模式已在《框架设计指南》中进行了充分描述,但MSDN还提供了一些如何实现的提示:

Implementors of the pattern use the AsyncOperationManager to create an AsyncOperation for each asynchronous operation and raise events using the AsyncOperation.Post Method . 该模式的实现者使用AsyncOperationManager创建AsyncOperation使用每个异步操作并引发事件AsyncOperation.Post方法 The Post Method executes the passed callback on the SynchronizationContext that was current at the time when the AsyncOperation was created. POST方法执行的传递回调的SynchronizationContext ,这是当前在当时的时候AsyncOperation创建。

The default SynchronizationContext in a WinForms or WPF application is the UI thread, or null in a Console application. WinForms或WPF应用程序中的默认SynchronizationContext是UI线程,在控制台应用程序中为null The WebClient Class apparently chooses to raise the events in a ThreadPool thread in the latter case, but this is an implementation detail. WebClient类显然选择在后一种情况下在ThreadPool线程中引发事件,但这是实现细节。

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

相关问题 C#在另一个线程上引发事件 - C# Raise events on another thread 在主UI线程上提升.NET中的事件 - Raise Events in .NET on the main UI thread 将事件从任务提升到UI线程 - Raise Events to the UI thread from a Task ApiController中的WebClient.DownloadFileAsync不会触发事件 - WebClient.DownloadFileAsync in ApiController does not trigger events 如果创建了一个对象,并且在一个线程中分配了事件,然后从另一个线程运行了该事件,那么该事件将在哪个线程上运行? - If an object is created and has events assigned in one thread and is then ran from a different thread on which thread does the event run? 在新线程中启动表单并在它们之间引发事件 - Start form in new thread and Raise events between them BackgroundWorker进程需要引发要在主(UI)线程上处理的自定义事件 - BackgroundWorker process needs to raise custom events to be handled on the main (UI) thread 是否可以将CoreDispatcher传递给Singleton以在UI线程上自动引发事件? - Is it OK to pass CoreDispatcher to a Singleton to auto-raise events on UI thread? SMO转移引发哪个例外? - Which exception does SMO Transfer Raise? ASP.NET如何在客户端上引发事件? - How does ASP.NET raise events on the client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM