异步和等待 - 控制台,Windows窗体和ASP.NET的区别 [英] Async and Await - Difference between Console, Windows form and ASP.NET

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

问题描述

我一直教育自己的异步/恭候使用,我想我明白在引擎罩的概念。然而,大多数频道9教程,MSDN文章和回答#1对异步/等待使用基于GUI应用程序(Windows窗体应用程序)来演示异步/等待的能力。不过,我注意到在异步/等待着一个根本的区别在UI线程中使用的应用程序与普通线程池线程的应用程序(如ASP.NET Web应用程序或控制台应用程序等)。

由于在UI线程的应用程序,UI线程始终可用(除非该过程明确地或通过视窗停止),所以线程池线程负责后执行code的等待,在任何异步方法,将保证找到UI线程发布结果送回(如果有的话)。

不过,在控制台应用程序或ASP.NET Web应用程序,主线程(在控制台应用程序)或HTTP请求(在ASP.NET Web应用程序),必须等待(在一个点的时间),直到所有异步操作完成。所以应该有.Wait()和。结果电话后某处异步方法调用,如果有什么更多的工作。

这是理解是否正确?我不怀疑有异步IO的约束或网络绑定操作(我明白怎么回事,以提高应用程序的可扩展性)的利益。

解决方案
  

由于在UI线程的应用程序,UI线程始终可用(除非该过程明确地或通过视窗停止),所以线程池线程负责后执行code的等待,在任何异步方法,将保证找到的UI线程后的结果反馈(如果有的话)。

这是稍有困惑。有没有迹象表明一个线程池线程将被要求在所有。

这是到awaitable实施,以确定运行的延续,但通常在当前的同步上下文的是用来工作在哪里运行它:

  • 在一个控制台应用程序,还有的的不同步的情况下,让一个线程池线程用于延续。
  • 在Windows窗体应用程序,当你在UI上正在运行的线程同步上下文是一种将执行code中的UI线程上...所以继续执行存在(除非你使用 ConfigureAwait ,等...)
  • 在ASP.NET应用程序中,有专门针对ASP.NET本身就是一个同步环境。

请参阅斯蒂芬·克利里的MSDN文章更多细节。

目前还不清楚你所说的你以后的问题是指关于有叫等待结果在ASP.NET或一个控制台应用程序......在这两种情况下它的可以的要求,但同样也可能不是。这取决于你在做什么真的。不要忘记,即使一个控制台应用程序就可以开始自己的线程和做各种其他的事情 - 你可以在一个控制台应用程序编写一个HTTP服务器,例如...

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 Stackoverflow answers on Async / Await use GUI based applications (Windows form application) to demonstrate the power of Async / Await. However, I noticed a fundamental difference in Async / Await use in UI thread based application vs. regular ThreadPool thread based applications (e.g. ASP.NET Web Application or Console Application etc.).

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).

However, in console application or ASP.NET Web application, the main thread (in console application) or the Http request (in 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.

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

解决方案

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).

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:

  • 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.

See Stephen Cleary's MSDN article for more details.

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...

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

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