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

后端 未结 2 1901
温柔的废话
温柔的废话 2021-01-17 15:44

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

  • If called from a

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 16:35

    The WebClient Class implements the Event-based Asynchronous Pattern. The pattern is fully described in the Framework Design Guidelines, but MSDN also provides a few hints how it is implemented:

    Implementors of the pattern use the AsyncOperationManager to create an AsyncOperation for each asynchronous operation and raise events using the AsyncOperation.Post Method. The Post Method executes the passed callback on the SynchronizationContext that was current at the time when the AsyncOperation was created.

    The default SynchronizationContext in a WinForms or WPF application is the UI thread, or null in a Console application. The WebClient Class apparently chooses to raise the events in a ThreadPool thread in the latter case, but this is an implementation detail.

提交回复
热议问题