A trap about .net asynchronous programming

原创 2016年10月20日 16:46:08
1.Code Sample & Result
What do you think about the result of the following codes?

I created a console project with the codes. The result is displayed as following:

It seems work well. But, I created a GUI project with the same codes. Calling the Test function in a message handler as following:

See the result as following:

It seems the codes are blocked at Task.Wait(). The windows is not responding.
2.Changed Sample & Result
Let's make a little changes as following:

I commented the calling of Task.Wait() in GUI project. See the result as following:

3.Changing Sample Code Again
If call the Test function in a user-created thread, how about the result? Let's change the sample again.

See the result as following:

The remaining part of asynchronous function after await keyword runs in the original thread automatically.
4.Analyzation
Comparing the difference of output between the 3 tests, we can find that the codes after await keyword runs in calling thread after asynchronous operation is completed in GUI process(if the asynchronous method is called in GUI thread). The calling thread is blocked by Task.Wait() and can't run the remaining part of asynchronous function after await keyword. It's the root cause of deadlock as following:

5.Solution
A simple solution is call the method Task.ConfigureAwait as following:

See the result as following:

The deadlock disappears. The remaining part of asynchronous function after await keyword is runned in the same thread as previous part. Please refer the article in 7th section for detailed solution.
6.Further Research
How does the process of the remaining part of asynchronous function after await keyword been transferred to the calling thread if we don't call the method ConfigureAwait? Let's seed the call stack while the asynchronous method is completed. We can find that the message mechanism supports the transfer between 2 threads.

7.References
《Async/Await - Best Practices in Asynchronous Programming》https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

相关文章推荐

Pro Asynchronous Programming with .NET

  • 2015年11月15日 16:13
  • 8.24MB
  • 下载

ASP.NET异步请求处理(Asynchronous HTTP Handlers)

ASP.NET异步请求处理(Asynchronous HTTP Handlers) ASP.NET中你可以通过继承IHttpHandler这个接口来实现一个同步(Synchronou...
  • nic7968
  • nic7968
  • 2012年05月28日 23:54
  • 306

java二维码生成与解析代码实现 <转载自http://blog.csdn.net/about58238/article/details/7494704>

二维码,是一种采用黑白相间的平面几何图形通过相应的编码算法来记录文字、图片、网址等信息的条码图片。如下图 二维码的特点: 1.  高密度编码,信息容量大 可容纳多达1850个大写字母或271...

Quartz.NET教程_Lesson 4: More About Triggers

课程4:触发器详解 如同作业一样,使用触发器也是很容易的一件事。但是触发器也包含一些需要客户端定制的选项,在充分使用Quartz.NET之前这些细节你必须充分注意并理解他们。同样的,在之前,我们曾提到...

Quartz.NET教程_Lesson 3: More About Jobs & JobDetails

课程3:详解作业和作业详情 当一个你实现的作业类具备了实现某种特定特定功能的代码,Quartz.NET框架需要知道关于你实现的这个作业类实例的各种属性。这些工作需要通过JobDetail类来实现,这在...

ASP.NET MVC 3 开发的20个秘诀(二十)[20 Recipes for Programming MVC 3]:缓存结果数据加速页面载入

http://www.cnblogs.com/o2ds/archive/2012/01/07/2315780.html   议题 随着网站的发展,大量用户访问流行内容和动态内容,这两个方面的因素...

Programming .NET Components 2nd 学习笔记(四)

4.3. .NET Garbage Collection 在.NET编程中,离开声明范围并不会销毁对象,但是不像COM,.NET不对对象使用引用计数。与之替代的,.NET有一个复杂的垃圾回收机制,当...
  • zjn0430
  • zjn0430
  • 2012年07月08日 22:17
  • 891

Using epoll() For Asynchronous Network Programming

General way to implement tcp servers is “one thread/process per connection”. But on high loads this ...

HttpWebRequest - Asynchronous Programming Model/Task.Factory.FromAsyc

潜水三年,今天终于忍不住开始了博客旅程。本人文笔不咋的。所以先从翻译外文开始吧。 声明:本篇文章为我第一次翻译外文,如有言辞不准确的地方,请大家查看原文,欢迎转载,但请注明出处,谢谢。 本篇文章主要讲...

Asynchronous Programming Design Patterns (to be continue)

http://msdn.microsoft.com/en-us/library/ms228975.aspx http://www.codeproject.com/Articles/67749...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:A trap about .net asynchronous programming
举报原因:
原因补充:

(最多只允许输入30个字)