Control Flow in Async Programs
Control Flow in Async Programs
You can write and maintain asynchronous programs more easily by using the Async and Await keywords.
However, the results might surprise you if you don't understand how your program operates.
This topic traces追溯 the flow of control through a simple async program to show you when control moves from one method to another and what information is transferred each time.
In general, you mark methods that contain asynchronous code with the Async (Visual Basic) or async (C#) modifier.
In a method that's marked with an async modifier, you can use an Await (Visual Basic) or await (C#) operator to specify where the method pauses to wait for a called asynchronous process to complete.
For more information, see Asynchronous Programming with Async and Await (C# and Visual Basic).
The following example uses async methods to download the contents of a specified website as a string and to display the length of the string.
The example contains the following two methods.
startButton_Click, which calls AccessTheWebAsync and displays the result.
AccessTheWebAsync, which downloads the contents of a website as a string and returns the length of the string.
AccessTheWebAsync uses an asynchronous HttpClient method, GetStringAsync(String), to download the contents.
Numbered display lines appear at strategic战略上的 points throughout the program to help you understand how the program runs and to explain what happens at each point that is marked.
The display lines are labeled "ONE" through "SIX."
The labels represent the order in which the program reaches these lines of code.
The following code shows an outline of the program.
需要添加对System.Net.Http.dll的引用,
private async void button1_Click(object sender, EventArgs e)
{
//One
Task<int> getLengthTask = AccessTheWebAsync(); //Four
int concentLength = await getLengthTask; //Six
textBox1.Text = $"Length of downloaded string is {concentLength}";
} private async Task<int> AccessTheWebAsync()
{
//Two
HttpClient httpClient = new HttpClient();
Task<string> getStringTask = httpClient.GetStringAsync("http://www.cnblogs.com/chucklu/"); //Three
string urlContents = await getStringTask;
Console.WriteLine(urlContents); //Five
return urlContents.Length;
}
Each of the labeled locations, "ONE" through "SIX," displays information about the current state of the program.
The following output is produced.
ONE: Entering startButton_Click.
Calling AccessTheWebAsync. TWO: Entering AccessTheWebAsync.
Calling HttpClient.GetStringAsync. THREE: Back in AccessTheWebAsync.
Task getStringTask is started.
About to await getStringTask & return a Task<int> to startButton_Click. FOUR: Back in startButton_Click.
Task getLengthTask is started.
About to await getLengthTask -- no caller to return to. FIVE: Back in AccessTheWebAsync.
Task getStringTask is complete.
Processing the return statement.
Exiting from AccessTheWebAsync. SIX: Back in startButton_Click.
Task getLengthTask is finished.
Result from AccessTheWebAsync is stored in contentLength.
About to display contentLength and exit. Length of the downloaded string: 33946.
Control Flow in Async Programs的更多相关文章
- Core Java Volume I — 3.8. Control Flow
3.8. Control FlowJava, like any programming language, supports both conditional statements and loops ...
- 《CS:APP》 chapter 8 Exceptional Control Flow 注意事项
Exceptional Control Flow The program counter assumes a sequence of values ...
- Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises
非常好的文章,讲javascript 的异步编程的. ------------------------------------------------------------------------- ...
- 异步 JS: Callbacks, Listeners, Control Flow Libs 和 Promises【转载+翻译+整理】
http://sporto.github.io/blog/2012/12/09/callbacks-listeners-promises/ 或 http://www.ruanyifeng.com/bl ...
- CSAPP Chapter 8:Exception Control Flow
prcesssor在运行时,假设program counter的值为a0, a1, ... , an-1,每个ak表示相对应的instruction的地址.从ak到ak+1的变化被称为control ...
- SSIS的 Data Flow 和 Control Flow
Control Flow 和 Data Flow,是SSIS Design中主要用到的两个Tab,理解这两个Tab的作用,对设计更高效的package十分重要. 一,Control Flow 在Con ...
- Control Flow 如何处理 Error
在Package的执行过程中,如果在Data Flow中出现Error,那么Data Flow component能够将错误行输出,只需要在组件的ErrorOutput中进行简单地配置,参考<D ...
- 关于Control flow
1.一个package包含一个control flow并且一个或多个data flow. (这个项目叫做 Integration services project,提供了三种不同类型的control ...
- SSIS ->> Control Flow And Data Flow
In the Control Flow, the task is the smallest unit of work, and a task requires completion (success, ...
随机推荐
- MAC 平台 QT编写iphone程序,加载iphone模拟器失败解决办法
本日这么多年一直做C++开发,最近要做QT项目,被QT做界面的新特性所吸引.QSS QML的确是亮点. 还有一个就是跨平台这方面,自己玩了玩. 用的QT 的开发包是在官网上下载 qt-opensour ...
- jquery定义表格宽度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- nullptr和NULL
nullptr是c++11中的关键字,表示空指针 要区分nullptr和NULL,首先要明白NULL的含义: NULL是一个宏定义,在c和c++中的定义不同,c中NULL为(void*)0,而c++中 ...
- 使用highcharts 绘制Web图表
问题描述: 使用highcharts 绘制Web图表 Highcharts说明: 问题解决: (1)安装Highcharts 在这些图表中,数据源是一个典型的JavaScrip ...
- 基于Hash算法的高维数据的最近邻检索
一.摘要 最紧邻检索:一种树基于树结构,一种是基于hash a.随机投影算法,需要产生很多哈希表,才能提高性能. b.基于学习的哈希算法在哈希编码较短时候性能不错,但是增加编码长度并不能显著提高性能. ...
- fedora下缺少autopoint包的解决办法
编译过程中,报错,缺少autopoint包 然而无论是yum install autopoint 还是yum search autopoint都没有理想的答案 执行yum install gettex ...
- Zabbix 安装及微信短信提醒
Zabbix简介 Zabbix 近几年得到了各大互联网公司的认可,当然第一点归功与它强大的监控功能,第二点免费开源也得到了广大用户的青睐.Zabbix 能将操作系统中的绝大部分指标进行监控,比如(CP ...
- URAL 1073 Square Country(DP)
题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 ...
- 用 Xamarin for VS 创建 aar 文件的绑定
预备工作:相关aar文件,Xamarin for VS一份.我这里以Android中挺火的 MaterialDesignLibrary 为例. 1.首先,创建一个Xamarin Binding Lib ...
- 典型重构3 (Try/Catch)
Try/Catch 块过多 public Customer GetCustomer(string customerId) { try { var command = new SqlCommand(); ...