博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
4.5 异步特性
阅读量:5231 次
发布时间:2019-06-14

本文共 2002 字,大约阅读时间需要 6 分钟。

private async void StartButton_Click(object sender, RoutedEventArgs e)        {            // ExampleMethodAsync returns a Task
and has an int result. // A value is assigned to intTask when ExampleMethodAsync reaches // an await. try { Task
intTask = ExampleMethodAsync(); // You can do other work here that doesn't require the result from // ExampleMethodAsync. . . . // You can access the int result when ExampleMethodAsync completes. int intResult = await intTask; // Or you can combine the previous two steps: //int intResult = await ExampleMethodAsync(); // Process the result (intResult). . . . } catch (Exception) { // Process the exception. . . . } } public async Task
ExampleMethodAsync() { var httpClient = new HttpClient(); // At the await expression, execution in this method is suspended, and // control returns to the caller of ExampleMethodAsync. // Variable exampleInt is assigned a value when GetStringAsync completes. int exampleInt = (await httpClient.GetStringAsync("http://msdn.microsoft.com")).Length; // You can break the previous line into several steps to clarify what happens: //Task
contentsTask = httpClient.GetStringAsync("http://msdn.microsoft.com"); //string contents = await contentsTask; //int exampleInt = contents.Length; // Continue with whatever processing is waiting for exampleInt. . . . // After the return statement, any method that's awaiting // ExampleMethodAsync can get the integer result. return exampleInt; }

 

 

转载于:https://www.cnblogs.com/LiMin/archive/2013/04/03/2997175.html

你可能感兴趣的文章
hihoCoder 1233 : Boxes(盒子)
查看>>
团队的绩效评估计划
查看>>
oracle中anyData数据类型的使用实例
查看>>
C++对vector里面的元素排序及取任意重叠区间
查看>>
软件测试——性能测试总结
查看>>
12.4站立会议
查看>>
Java Concurrentmodificationexception异常原因和解决方法
查看>>
客户端访问浏览器的流程
查看>>
codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)
查看>>
c++||template
查看>>
[BZOJ 5323][Jxoi2018]游戏
查看>>
编程面试的10大算法概念汇总
查看>>
Vue
查看>>
python-三级菜单和购物车程序
查看>>
条件断点 符号断点
查看>>
VMware12 + Ubuntu16.04 虚拟磁盘扩容
查看>>
设计模式——设计模式概述
查看>>
封装一个获取module.exports内容的方法
查看>>
动态连接库
查看>>
ServletContext 与application的异同
查看>>