Control Flow in Async Programs
2015-12-15 13:54
465 查看
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的引用,
Each of the labeled locations, "ONE" through "SIX," displays information about the current state of the program. The following output is produced.
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.
相关文章推荐
- 【转】 Pro Android学习笔记(九一):了解Handler(5):组件生命
- 【转】 Pro Android学习笔记(九十):了解Handler(4):Worker线程
- LeetCode 26:Remove Duplicates from Sorted Array
- 分布式搜索Elasticsearch——概述
- iOS GitHub上值得留意的第三方库
- 自定义UIAlertView
- 【转】 Pro Android学习笔记(八九):了解Handler(3):延迟执行小例子
- 【转】 Pro Android学习笔记(八八):了解Handler(2):什么是Handler
- c#.net网页跳转七种方法
- 带宏函数等excel解析方法
- 龙珠直播CTO谭振林:从0到500万用户背后的技术架构变迁
- getaddrinfo简单应用——取得IP地址
- C# .net中cookie值为中文时的乱码解决方法
- iReport学习笔记
- 【待解决】maven创建web项目报错
- 更改Activity亮度
- 【转】 Pro Android学习笔记(八七):了解Handler(1):组件和线程
- 【转】 Pro Android学习笔记(八六):了解Package(5):使用lib
- 百度地图 Android SDK - 标注(Marker)的基本使用
- 【转】 Pro Android学习笔记(八五):了解Package(4):lib项目