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

异步编程(Asynchronous Programming)

异步编程(Asynchronous Programming)   异步编程与我们所看过的其他并行编程形式有一些不同,讨论的其他主题可以有大量的线程并行运行,可以完全利用系统中可用的处理器;而在异步编程...
  • hadstj
  • hadstj
  • 2014年05月15日 10:26
  • 2633

异步编程模型(APM,Asynchronous Programming Model)

异步编程模型(APM,Asynchronous Programming Model) 优点: 1. 将资源使用率降低,因为如果是同步的话,会发生阻塞而导致在线程池中启动多个线程,而CLR线程占用资...
  • jiayp004
  • jiayp004
  • 2013年02月17日 10:22
  • 685

Asynchronous Programming Model in .NET 2.0

Asynchronous Programming Model (APM) in.NET 2.0 Xiaolin (Colin) Peng WitStream Technologies Inc.  1....
  • xiaolinpeng
  • xiaolinpeng
  • 2007年05月15日 00:11
  • 827

Introduction to Asynchronous Programming in .NET 2.0

Introduction to Asynchronous programming Xiaolin (Colin) Peng WitStream Technologies Inc.     1.1 In...
  • xiaolinpeng
  • xiaolinpeng
  • 2007年05月15日 00:01
  • 575

Asynchronous programming

Asynchronous programming is much more of a philosophy than just another programming trick. While, yo...
  • chao56789
  • chao56789
  • 2017年08月29日 11:56
  • 122

强化学习中同步动态规划和异步动态规划 Synchronous and Asynchronous dynamic programminging

同步动态规划是基础:一次性更新所有的S的value。异步动态规划分集中常见类型: in-place 动态规划: 不对上一周期的value进行备份,直接使用这一周期的value(当然,本周期...
  • zhaoying9105
  • zhaoying9105
  • 2017年06月19日 15:21
  • 797

Codeforces 366D Dima and Trap Graph 【并查集】

题目链接:Codeforces 366D Dima and Trap Graph D. Dima and Trap Graph time limit per test3 seconds memo...
  • chenzhenyu123456
  • chenzhenyu123456
  • 2016年03月31日 15:08
  • 398

Codeforces 366D Dima and Trap Graph(搜索剪枝/二分)

题目地址:点击打开链接 题意:给你一个图,每条边有一个[L, R],只有在这个区间内的点才能通过,问满足能从1 到n的最大连续区间的长度为多少 思路: 二分思路:枚举下界,二分上界,dfs是否...
  • CillyB
  • CillyB
  • 2017年03月07日 20:47
  • 355

Delphi 通信报Asynchronous socket error 10053错误的一个解决方法

在使用Delphi中的TClientSocket和TServerSocket进行通信编程时,当使用TClientSocket这方的客户端使用TClientSocket.close断开连接并退出程序时,...
  • shuiyuanfengxing
  • shuiyuanfengxing
  • 2013年01月15日 09:56
  • 9027

inlineCallbacks: A New Way towards Asynchronous Programming

异步编程,是目前解决性能问题的一个大方向。其中怎么样实现异步有多种不同的实现方式。通过异步的方式,能够实现更高的资源利用和响应性。在网络和图形界面编程里面,一种非常普遍的做法是基于事件来实现用户响应性...
  • xinghun_4
  • xinghun_4
  • 2015年07月05日 10:57
  • 851
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:A trap about .net asynchronous programming
举报原因:
原因补充:

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