Async/Await - Best Practices in Asynchronous Programming
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
Figure 1 Summary of Asynchronous Programming Guidelines
Name | Description | Exceptions |
Avoid async void | Prefer async Task methods over async void methods | Event handlers |
Async all the way | Don’t mix blocking and async code | Console main method |
Configure context | Use ConfigureAwait(false) when you can | Methods that require context |
Avoid Async Void
You should prefer "async Task" to "async void". Async Task methods enable easier error-handling(propagate up or not), composability (Task.waitAll ...) and testability. The exception to this guideline is asynchronous event handlers, which must return void. This exception includes methods that are logically event handlers even if they’re not literally event handlers (for example, ICommand.Execute implementations).
Async All the Way
"Async all the way” means that you shouldn’t mix synchronous and asynchronous code without carefully considering the consequences. In particular, it’s usually a bad idea to block on async code by calling Task.Wait or Task.Result. This is an common problem for programmers who try to convert just a small part of their application and wrapping it in a synchronous API so the rest of the application is isolated from the changes. Unfortunately, this can cause deadlocks, in the case of GUI or ASP.NET (not if in a console application). The exception semantic for await and Task.Wait is also different, Exception versus AggregateException. So do not do this except in the Main method for console applications.
Figure 5 The “Async Way” of Doing Things
To Do This … | Instead of This … | Use This |
Retrieve the result of a background task | Task.Wait or Task.Result | await |
Wait for any task to complete | Task.WaitAny | await Task.WhenAny |
Retrieve the results of multiple tasks | Task.WaitAll | await Task.WhenAll |
Wait a period of time | Thread.Sleep | await Task.Delay |
Configure Context
Await require context, see the following code, if you swap the commented-out lines in DelayAsync, it will not deadlock,
public static class DeadlockDemo
{
private static async Task DelayAsync()
{
await Task.Delay(1000);
//await Task.Delay(1000).ConfigureAwait(continueOnCapturedContext: false);
} // This method causes a deadlock when called in a GUI or ASP.NET context.
public static void Test()
{ // Start the delay.
var delayTask = DelayAsync(); // Wait for the delay to complete.
delayTask.Wait();
}
}
This technique is particularly useful if you need to gradually convert an application from synchronous to asynchronous.
You should not use ConfigureAwait when you have code after the await in the method that needs the context.
Async/Await - Best Practices in Asynchronous Programming的更多相关文章
- Async/Await - Best Practices in Asynchronous Programming z
These days there’s a wealth of information about the new async and await support in the Microsoft .N ...
- [译]Async/Await - Best Practices in Asynchronous Programming
原文 避免async void async void异步方法只有一个目的:使得event handler异步可行,也就是说async void只能用于event handler. async void ...
- Best Practices in Asynchronous Programming
http://blog.stephencleary.com/ http://blogs.msdn.com/b/pfxteam/
- 将 async/await 异步代码转换为安全的不会死锁的同步代码
在 async/await 异步模型(即 TAP Task-based Asynchronous Pattern)出现以前,有大量的同步代码存在于代码库中,以至于这些代码全部迁移到 async/awa ...
- Async/Await FAQ
From time to time, I receive questions from developers which highlight either a need for more inform ...
- 异步模式:Callbacks, Promises & Async/Await
[译]异步JavaScript的演变史:从回调到Promises再到Async/Await https://www.i-programmer.info/programming/theory/8864- ...
- C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)
https://msdn.microsoft.com/zh-cn/library/mt674882.aspx 侵删 更新于:2015年6月20日 欲获得最新的Visual Studio 2017 RC ...
- Asynchronous programming with async and await (C#)
Asynchronous Programming with async and await (C#) | Microsoft Docs https://docs.microsoft.com/en-us ...
- .NET 基于任务的异步模式(Task-based Asynchronous Pattern,TAP) async await
本文内容 概述 编写异步方法 异步程序中的控制流 API 异步方法 线程 异步和等待 返回类型和参数 参考资料 下载 Demo 下载 Demo TPL 与 APM 和 EAP 结合(APM 和 EAP ...
随机推荐
- PreparedStatement
PreparedStatement > 它是Statement接口的子接口: >强大之处: 防SQL攻击: 提高代码的可读性.可维护性: 提高效率! l 学习PreparedStateme ...
- 第六章 第一个Linux驱动程序:统计单词个数
现在进入了实战阶段,使用统计单词个数的实例让我们了解开发和测试Linux驱动程序的完整过程.第一个Linux驱动程序是统计单词个数. 这个Linux驱动程序没有访问硬件,而是利用设备文件作为介质与应用 ...
- PHP面试题之驼峰字符串转换成下划线样式例子
自己在看到这个问题的时候,想到的是用ASCII码来处理,没往万能的正则上去想.好吧,下面来看看答案: 答案1: 代码如下 复制代码 $str = 'OpenAPI'; $length = mb_str ...
- android自定义按键
android自带菜单键.返回键.搜索键的重写 转自:http://blog.sina.com.cn/s/blog_7cb9b3b801015yk8.html 返回键 public void on ...
- 团队开发——冲刺2.g
冲刺阶段二(第七天) 1.昨天做了什么? 编写软件计划书第三阶段:整理用户体验建议:据用户对界面的要求,把小球改头换面,借鉴超级马里奥叔叔的道具们. 2.今天准备做什么? 最后的美工,统一整合: 测试 ...
- 安卓--Toast
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWind ...
- 基于.NET的CAD二次开发学习笔记一:CAD开发入门
1.AutoCAD .NET API由不同的DLL文件组成,它们提供用于访问图形文件或AutoCAD应用程序的包含丰富的类.结构.方法和事件.每一个DLL文件都定义不同的使用基于功能的库组织组件的命名 ...
- WCF XmlSerializer GetType 反射类型 报错 解决方案
为图省事,用现有的EF,和web一起使用一个Model类 当进行到 XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); 会报 ...
- jsp内置对象浅谈
jsp内置对象浅谈 | 浏览:1184 | 更新:2013-12-11 16:01 JSP内置对象:我们在使用JSP进行页面编程时可以直接使用而不需自己创建的一些Web容器已为用户创建好的JSP内置对 ...
- HDU 1710
http://acm.hdu.edu.cn/showproblem.php?pid=1710 题意:给二叉树的先序遍历和中序遍历,确定后序遍历 解法:复习专业课找的一题,根据先序遍历和中序遍历建树,再 ...