In .net 4.5, there are new keyword, which are await and async. which is used widely in the asynchronous programming.
In this post we are going to examining in the language level , what is the requirement on the return types on await calls and the async method. (can we have async dynamic method? I guess yes, but will need to verify that).
You can find the original discussion on the return types of such async/await method in the Async Return Types.
Most of the code is a reinvigorate of the sample code that you can find from Async Return Types. but with my own comment and annotation..
namespace AsyncResturnTypes { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } // -- Void Return example private async void button1_Click(object sender, RoutedEventArgs e) { textBox1.Clear(); // Start the process and await its completion. DriverAsync is a // Task-returning async method. await DriverAsync(); // Say goodbye. textBox1.Text += "\r\nAll done, exiting button-click event handler."; } async Task DriverAsync() { // Task<T> // Call and await the Task<T>-returning async method in the same statement. int result1 = await TaskOfT_MethodAsync(); // call and await in separate statements Task<int> integerTask = TaskOfT_MethodAsync(); // You can do other work that does not rely on integerTask before awaiting. textBox1.Text += String.Format("Application can continue working while the Task<T> runs. . . . \r\n"); int result2 = await integerTask; // Display the values of the result1 variable, the result2 variable, and // the integerTask.Result property. textBox1.Text += String.Format("\r\nValue of result1 variable: {0}\r\n", result1); textBox1.Text += String.Format("Value of result2 variable: {0}\r\n", result2); textBox1.Text += String.Format("Value of integerTask.Result: {0}\r\n", integerTask.Result); // Task // Call and await the Task-returning async method in the same statement. await Task_MethodAsync(); // Call and await in separate statements. Task simpleTask = Task_MethodAsync(); // You can do other work that does not rely on simpleTask before awaiting. textBox1.Text += String.Format("\r\nApplication can continue working while the Task runs. . . .\r\n"); await simpleTask; // -- Async methods that don't contain a return statement or that contain a return statement that doesn't return an operand usually have a return type of Task. Such methods would be void-returning methods } // TASK<T> EXAMPLE async Task<int> TaskOfT_MethodAsync() { // The body of the method is expected to contain an awaited asynchronous // call. // Task.FromResult is a placeholder for actual work that returns a string. var today = await Task.FromResult<string>(DateTime.Now.DayOfWeek.ToString()); // as in the "TASK EXAMPLE" case, you will need to have at least some await call somewhere and it shall be covered by all cases. // The method then can process the result in some way. int leisureHours; if (today.First() == 'S') leisureHours = 16; else leisureHours = 5; // Because the return statement specifies an operand of type int, the // method must have a return type of Task<int>. return leisureHours; // -- the C# runtime has this weaven ability, though you were returning this "int" result, it will wraps and return such an Task<int> types to match with the prototypes.. } // TASK EXAMPLE async Task Task_MethodAsync() { // The body of an async method is expected to contain an awaited // asynchronous call. // Task.Delay is a placeholder for actual work. await Task.Delay(2000); // if the containing method is "void" , you will see compiling errors. // so it is a prerequisite to have this await method. // Task.Delay delays the following line by two seconds. textBox1.Text += String.Format("\r\nSorry for the delay. . . .\r\n"); // This method has no return statement, so its return type is Task. } }
and this is the xaml code files.
<Window x:Class="AsyncResturnTypes.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <!-- source code from : Async Return types... http://msdn.microsoft.com/en-us/library/vstudio/hh524395.aspx --> <Grid> <Button x:Name="button1" Content="Start" HorizontalAlignment="Left" Margin="214,28,0,0" VerticalAlignment="Top" Width="75" HorizontalContentAlignment="Center" FontWeight="Bold" FontFamily="Aharoni" Click="button1_Click"/> <TextBox x:Name="textBox1" Margin="0,80,0,0" TextWrapping="Wrap" FontFamily="Lucida Console"/> </Grid> </Window>
相关推荐
前端开源库-jsdoc-strip-async-awaitjsdoc strip async wait,jsdoc plugin to strip async/wait keywords before parse.
JavaScript异步编程指南-终极解决方案Async-Await.docx
编写更好的async await并避免try catch语句如果使用以下预设则此插件可以很好。 :light_bulb: 对于与CRA以及任何依赖@ babel / env或依赖于预设和插件的应用程序一起使用,我们强烈建议使用 注意:如果使用的是...
js--promise、async 和 await 相关知识总结.doc
EA Async在JVM中实现Async-Await方法。 它允许程序员以顺序的方式编写异步代码。 它在很大程度上受到.NET CLR上Async-Await的启发,有关更多信息,请参见进行。 谁应该使用它? 应该使用EA Async编写非阻塞异步...
Async-and-Await:使用async和await替换promise就像从es6迁移到es7一样,猜您已经知道了,这更多地说明了如何将其用于自定义http请求
01-async-await.html
昨天有朋友在公众号发消息说看不懂await,async执行流,其实看不懂太正常了,因为你没经过社会的毒打,没吃过牢饭就不知道自由有多重要,没生过病就不知道健康有多重要,没用过ContinueWith就不知道await,async有多...
embedded metadata, reflection, and attributes* Thoroughly mastering multithreading and synchronization, including the new async/await paradigm* Discussion of WinRT and programming in C# for Windows 8...
async-await-nodejs的启动库
众所周知C#提供Async和Await关键字来实现异步编程。在本文中,我们将共同探讨并介绍什么是Async 和 Await,以及如何在C#中使用Async 和 Await。 同样本文的内容也大多是翻译的,只不过加上了自己的理解进行了相关...
taro3.x react redux await-async promise api 能运行的工程h5/小程序
C#异步方法的实现(async和await)入门示例
Babel插件可使用最少的帮助器函数将包含await表达式的async函数转换为等效的Promise调用链。 输入: async function fetchAsObjectURL ( url ) { const response = await fetch ( url ) ; const blob = await ...
C#5.0 中async 和await 两个关键字,这两个关键字简化了异步编程
前端开源库-eslint-plugin-asynceslint插件async,>async/await-linting-rules for eslint
主要介绍了C#中的Async和Await用法,包括在C#5.0下一些新特性的影响,需要的朋友可以参考下
yarn add -D babel-plugin-await-import-from-cdn @babel/plugin-syntax-top-level-await 或者 npm i -D babel-plugin-await-import-from-cdn @babel/plugin-syntax-top-level-await 用法 module . exports = { ...
自己写的一个winform小示例 用非常简短的几行代码演示C#5.0的异步编程新特性 不参杂其他无关代码,浅显易懂
Async 函数作为异步解决方案的最优解,async/await 特性能让我们编写出相比回调地狱和 Promise 链式调用更直观、更容易理解的代码,Async 函数返回一个 Promise 对象,可以使用 then() 方法添加回调函数,当函数执行...