Async 和 await - 控制台、Windows 窗体和 ASP.NET 之间的区别 [英] Async and await - difference between console, Windows Forms and ASP.NET

查看:26
本文介绍了Async 和 await - 控制台、Windows 窗体和 ASP.NET 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在对自己进行异步/等待使用方面的教育,我想我理解了幕后的概念.但是,大多数 Channel 9 教程、MSDN 文章和有关 async/await 的堆栈溢出答案都使用基于 GUI 的应用程序(Windows 窗体应用程序)来演示 async/await 的强大功能.

I have been educating myself on async / await use and I think I understood under-the-hood concept. However, most of Channel 9 tutorials, MSDN articles and Stack overflow answers on async / await use GUI-based applications (Windows Forms application) to demonstrate the power of async / await.

但是,我注意到在基于 UI 线程的应用程序与基于常规 ThreadPool 线程的应用程序(例如 ASP.NET Web 应用程序、控制台应用程序等)中使用 async/await 的根本区别.

However, I noticed a fundamental difference in async / await use in a UI-thread based application vs. regular ThreadPool thread-based applications (e.g. ASP.NET Web Application, Console Application, etc.).

由于在基于 UI 线程的应用程序中,UI 线程始终可用(除非进程被显式停止或被 Windows 停止),因此负责执行任何异步方法中await"后的代码的 ThreadPool 线程,将保证找到 UI 线程将结果发回(如果有).

Since, in UI thread-based application, the UI thread is always available (unless the process is stopped explicitly or by Windows), so the ThreadPool thread responsible for executing the code after "await" in any async method, will guarantee to find the UI thread to post the results back (if any).

但是,在控制台应用程序或 ASP.NET Web 应用程序中,主线程(在控制台应用程序中)或 HTTP 请求(在 ASP.NET Web 应用程序中)必须等待(在某个时间点),直到所有异步操作完成.因此,如果没有其他工作要做,那么在 Async 方法调用之后的某处应该有 .Wait() 和 .Result 调用.

However, in a console application or ASP.NET Web application, the main thread (in a console application) or the HTTP request (in an ASP.NET web application) must be waiting (at one point of time) until all async operations are completed. So there should be .Wait() and .Result call somewhere after the Async method call, if there is nothing more to work on.

这种理解正确吗?我不会质疑对 I/O 绑定或网络绑定操作进行异步的好处(我了解它将如何提高应用程序的可扩展性).

Is this understanding correct? I am not questioning the benefit of having async for I/O bound or network-bound operations (I understand how it's going to increase application scalability).

推荐答案

由于在基于 UI 线程的应用程序中,UI 线程始终可用(除非进程被显式停止或被 Windows 停止),因此负责在任何异步方法中await"之后执行代码的 ThreadPool 线程将保证找到 UI 线程将结果发回(如果有).

Since, in UI thread based application, the UI thread is always available (unless the process is stopped explicitly or by Windows), so the ThreadPool thread responsible for executing the code after "await" in any async method, will guarantee to find the UI thread to post the results back (if any).

这有点令人困惑.没有迹象表明根本需要 ThreadPool 线程.

This is slightly confused. There's no indication that a ThreadPool thread will be required at all.

由可等待的实现来决定在哪里运行延续,但通常当前同步上下文用于确定在哪里运行它:

It's up to the awaitable implementation to determine where to run the continuation, but normally the current synchronization context is used to work out where to run it:

  • 在控制台应用程序中,没有同步上下文,因此使用线程池线程进行延续.
  • 在 Windows 窗体应用程序中,当您在 UI 线程上运行时,同步上下文是一个将在 UI 线程上执行代码的上下文......因此继续在那里执行(除非您使用 ConfigureAwait,等等...)
  • 在 ASP.NET 应用程序中,有一个特定于 ASP.NET 本身的同步上下文.
  • In a console application, there is no synchronization context, so a thread pool thread is used for the continuation.
  • In a Windows Forms application, when you're running on the UI thread the synchronization context is one which will execute code on the UI thread... so the continuation executes there (unless you use ConfigureAwait, etc...)
  • In an ASP.NET application, there's a synchronization context specific to ASP.NET itself.

有关详细信息,请参阅Stephen Cleary 的 MSDN 文章.

See Stephen Cleary's MSDN article for more details.

不清楚您稍后提出的关于必须在 ASP.NET 或控制台应用程序中调用 WaitResult 的意思......在这两种情况下它可能是必需的,但同样也可能不是.这取决于你真正在做什么.不要忘记,即使是控制台应用程序也可以启动自己的线程并执行各种其他操作 - 例如,您可以在控制台应用程序中编写 HTTP 服务器...

It's not clear what you mean by your later question about having to call Wait or Result in ASP.NET or a console app... in both scenarios it may be required, but equally it may not be. It depends on what you're doing really. Don't forget that even a console app can start its own threads and do all kinds of other things - you can write an HTTP server in a console app, for example...

这篇关于Async 和 await - 控制台、Windows 窗体和 ASP.NET 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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