await之后的线程问题
之前看了园子里的一篇文章「async & await的前世今生」,收益颇多。而其中有句话被博主特意用红色标注,所以留意多看了几眼,「await 之后不会开启新的线程(await 从来不会开启新的线程)」。在MSDN上找到的相关资料也佐证了其正确性——The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.(async 和 await 关键字不会导致创建其他线程。 因为异步方法不会在其自身线程上运行,因此它不需要多线程。 只有当方法处于活动状态时,该方法将在当前同步上下文中运行并使用线程上的时间。)
再建立一个Windows Forms应用工程,写点代码更形象地说明问题:
private void Form1_Load(object sender, EventArgs e)
{
PrintDataAsync();
Debug.Print("three");
} private async void PrintDataAsync()
{
Task<int> result = CalculateDataAsync();
Debug.Print("second");
int data = await result;
Debug.Print("last:" + data);
} private async Task<int> CalculateDataAsync()
{
Debug.Print(string.Format("{0} : {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread));
Debug.Print("first");
int result = 0;
for (int i = 0; i < 10; i++)
{
result += i;
}
await Task.Delay(1000);
Debug.Print("four");
Debug.Print(string.Format("{0} : {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread));
return result;
};
程序的结果如预期的一样,Output窗口中可以看到以下内容:
8 : False
first
second
three
four
8 : False
last:45
await之前的ManagedThreadId值与之后的ManagedThreadId值一致,IsThreadPoolThread始终是False,说明当前线程没有发生改变,也没有产生新的线程。
但如果建立的是Console应用工程,结果就不同了。
static void Main(string[] args)
{
PrintDataAsync();
Console.WriteLine("three");
Console.Read();
} private static async void PrintDataAsync()
{
Task<int> result = CalculateDataAsync();
Console.WriteLine("second");
int data = await result;
Console.WriteLine("last:" + data);
} private static async Task<int> CalculateDataAsync()
{
Console.WriteLine(string.Format("{0} : {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread));
Console.WriteLine("first");
int result = 0;
for (int i = 0; i < 10; i++)
{
result += i;
}
await Task.Delay(1000);
Console.WriteLine("four");
Console.WriteLine(string.Format("{0} : {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread));
return result;
}
这段代码的执行结果:
8 : False
first
second
three
four
10 : True
last:45
ManagedThreadId在await之后发生了变化,IsThreadPoolThread也变为了True,说明不是同一个线程。
为什么会这样?再看一下MSDN中描述——「The method runs on the current synchronization context and uses time on the thread only when the method is active」,这里涉及到SynchronizationContext对象的使用。
在Windows Forms工程代码中加入 Debug.Print(SynchronizationContext.Current.ToString()); 检测代码,其输出是System.Windows.Forms.WindowsFormsSynchronizationContext。
而如果在Console工程中加入类似的检测代码 Console.WriteLine(SynchronizationContext.Current.ToString()); 则会抛出空引用异常,因为SynchronizationContext.Current在Console工程中的值为null。
又从MSDN Magazine找到SynchronizationContext相关的文章,其中有介绍到:By convention, if a thread’s current SynchronizationContext is null, then it implicitly has a default SynchronizationContext.(根据惯例,如果一个线程的当前 SynchronizationContext 为 null,那么它隐式具有一个默认 SynchronizationContext。)The default SynchronizationContext is applied to ThreadPool threads unless the code is hosted by ASP.NET.(默认 SynchronizationContext 应用于 ThreadPool 线程,除非代码由 ASP.NET 承载。)
这里提到了APS.NET,所以再建个Web Forms应用工程用于验证:
protected void Page_Load(object sender, EventArgs e)
{
PrintDataAsync();
Debug.Print("three");
} private async void PrintDataAsync()
{
Debug.Print(SynchronizationContext.Current.ToString());
Task<int> result = CalculateDataAsync();
Debug.Print("second");
int data = await result;
Debug.Print("last:" + data);
} private async Task<int> CalculateDataAsync()
{
Debug.Print(string.Format("{0} : {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread));
Debug.Print("first");
int result = 0;
for (int i = 0; i < 10; i++)
{
result += i;
}
await Task.Delay(1000);
Debug.Print("four");
Debug.Print(string.Format("{0} : {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread));
return result;
}
输出结果:
System.Web.AspNetSynchronizationContext
8 : True
first
second
three
four
9 : True
last:45
ManagedThreadId值发生改变,IsThreadPoolThread始终是True,SynchronizationContext.Current值为System.Web.AspNetSynchronizationContext。
由三次试验及相关资料可以得出结论,await之后的线程依据SynchronizationContext在不同环境中的不同定义而产生不同的结果。所以「await 之后不会开启新的线程(await 从来不会开启新的线程)」的肯定句式改成「await 之后会开启新的线程吗? Maybe」这样的句式更加合适些。
最后补充一点,若是把第一个Windows Forms工程的代码 await Task.Delay(); 改成 await Task.Delay().ConfigureAwait(false); 的话,则可以得到第二个Console工程同样的结果。
await之后的线程问题的更多相关文章
- C# 中await前后执行线程的问题
悬赏园豆:20 [已解决问题] 浏览: 1763次 解决于 2018-08-15 22:43 今天有点疑惑就写了个测试的代码,发现控制台和Winform中不一样 比如: 控制台: ...Main( ...
- 浅谈C#中的 async await 以及对线程相关知识的复习
C#5.0以后新增了一个语法糖,那就是异步方法async await,之前对线程,进程方面的知识有过较为深入的学习,大概知道这个概念,我的项目中实际用到C#异步编程的场景比较少,就算要用到一般也感觉T ...
- java 线程 Lock 锁使用Condition实现线程的等待(await)与通知(signal)
一.Condition 类 在前面我们学习与synchronized锁配合的线程等待(Object.wait)与线程通知(Object.notify),那么对于JDK1.5 的 java.util.c ...
- async与await线程分配研究
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#语法——泛型的多种应用 C#语法——await与async的正确打开方式 C#线程安全使用(五) C#语法——元组类型 好好耕耘 redis和memcached的区别
C#语法——泛型的多种应用 本篇文章主要介绍泛型的应用. 泛型是.NET Framework 2.0 版类库就已经提供的语法,主要用于提高代码的可重用性.类型安全性和效率. 泛型的定义 下面定义了 ...
- 四、线程同步之Lock和Condition
Lock同步锁 Lock 在jdk1.5 提供了Lock以便执行同步操作,和synchronized不同的是Lock提供了显示的方法获取锁和释放锁.Lock提供了以下几个方法,请求和释放锁: voi ...
- C#~异步编程再续~await与async引起的w3wp.exe崩溃
返回目录 最近怪事又开始发生了,IIS的应用程序池无做挂掉,都指向同一个矛头,async,threadPool,Task,还有一个System.NullReferenceException,所以这些都 ...
- async 和 await小结
三大返回值: 返回类型 - Task<TResult> 返回类型 - Task 返回类型 - void 当你添加 async 关键字后,需要返回一个将用于后续操作的对象,请使用 Task& ...
- Java线程间通信方式剖析——Java进阶(四)
原创文章,同步发自作者个人博客,转载请在文章开头处以超链接注明出处 http://www.jasongj.com/java/thread_communication/ CountDownLatch C ...
随机推荐
- VS2016 发布项目提示 CS0006 C# Metadata file 'xxxxxxx.dll' could not be found
生成的时候没有问题,发布项目的时候提示 CS0006 C# Metadata file 'xxxxxxx.dll' could not be found 解决方案,找到 xxxxxxx.dll 这个项 ...
- Git 在团队中的最佳实践--如何正确使用Git Flow[转]
原文地址:http://www.cnblogs.com/cnblogsfans/p/5075073.html Git的优点 Git的优点很多,但是这里只列出我认为非常突出的几点. 由于是分布式,所有本 ...
- DailyRollingFileAppender的使用
DailyRollingFileAppender是日志记录软件包Log4J中的一个Appender,它能够按一定的频度滚动日志记录文件. 如果您不熟悉Log4J,建议阅读一下 使用Log4j进行日志记 ...
- 为何jquery动态添加的input value无法提交到数据库?【坑】
有两个输入框,我想让第一个输入框失去焦点以后,第二个输入框自动获取第一个输入框的value为默认值,jquery代码如下,可以正常显示,但是用PHP提交数据,并插入数据库的时候确实空值,尚未查找到原因 ...
- #include <cstdio>
#include <cstdio> using namespace std; int main() { int gx; gx=6; printf("%d\n",gx); ...
- MFC学习笔记
获取窗口句柄 FindWindow 根据窗口名获取 GetSafehWnd 取你程序所在窗口类的句柄 GetActiveWindow ...
- en_windows_10_multiple_editions_version_1511_x64.iso
好久没折腾电脑了,这几天在E盘装了个64位Windows 10 TH2 专业版,从MSDN官网下载的英文原版镜像,用kms10未能激活,一看日志文件,说我这是零售版,后面就关掉了什么监听端口,然后就完 ...
- iOS判断数组不为空
用([array count]==0 )来判断是否为空,都是坑,如果array为空的话,执行count就会直接报错,程序崩溃退出. 正确判断NSArray是否为空的方法: if(array != ni ...
- Jquery常用radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关设置
获取一组radio被选中项的值:var item = $('input[name=items][checked]').val(); 获取select被选中项的文本:var item = $(" ...
- SQL 重置自增列的值 批量处理
Declare @IdentityTable sysname, @IdentityColumn sysname, @TotalRows int, @i int, @Iden int, @Sql var ...