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.

最新文章

  1. SQL-2008函数大全
  2. web前端职业规划(转)
  3. Tomcat:基于Apache+Tomcat的集群搭建
  4. iOS 自动布局小结
  5. html5 canvas 标签
  6. bzoj 2286: [Sdoi2011消耗战
  7. 内部技术分享的 PPT
  8. Expectation Maximization and GMM
  9. [Android阅读代码]android-async-http源码学习一
  10. (转)log4j(三)——如何控制不同级别的日志信息的输出?
  11. UrlRewriter配置IIS支持伪静态
  12. [Swift]LeetCode910. 最小差值 II | Smallest Range II
  13. Lua语言自学之01.基础概念的理解
  14. Windows server 2012 install .net core sdk 2.2.103
  15. SpringBoot 添加fastjson
  16. linux中awk命令学习
  17. innosetup完整脚本
  18. 【ActiveMQ入门-4】ActiveMQ学习-异步接收
  19. 互评Beta版本——杨老师粉丝群——Pinball
  20. SDWebimage清空缓存

热门文章

  1. python学习笔记30(全局变量的两种解决办法)
  2. 服务器端spice配置详解
  3. Asp.net弹出层并且有遮罩层
  4. 打造自己的程序员品牌(摘自Infoq)
  5. 关于拓扑排序(topologicalsort)
  6. EXT经验--查询EditorGridPanel的tbar的默认配置对象
  7. Asp.Net原理Version1.0
  8. 【WCF--初入江湖】10 序列化和传输大型数据流
  9. POJ1144 Network 无向图的割顶
  10. Bash的脚本参数