星空网 > 软件开发 > ASP.net

C# Async与Await用法

C# Async与Await用法

C# Async与Await用法C# Async与Await用法
 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7  8 namespace CloudLearning.UI 9 {10   class Program11   {12     static void Main(string[] args)13     {14       AsyncPrintHelloWorld();15       Console.ReadLine();16     }17 18     public async static void AsyncPrintHelloWorld()19     {20       Console.WriteLine("异步方法调用开始"); 21       var result1 = TMothd1();22       var result2 = TMothd2();23       var result3 = TMothd3();24       Console.WriteLine("异步方法完成");25       int r1 = await result1;26       int r2= await result2;27       int r3 = await result3;28       Console.WriteLine("{0},{1},{2}", r1, r2, r3);29     }30 31     public async static Task<int> TMothd1()32     {33       return await Task.Factory.StartNew(() =>34       {35         Thread.Sleep(5 * 1000);36         Console.WriteLine("TMothd1 完成");37         return 1;38       });39     }40 41     public async static Task<int> TMothd2()42     {43       return await Task.Factory.StartNew(() =>44       {45         Thread.Sleep(10 * 1000);46         Console.WriteLine("TMothd2 完成");47         return 2;48       });49     }50     public async static Task<int> TMothd3()51     {52       return await Task.Factory.StartNew(() =>53       {54         Thread.Sleep(15 * 1000);55         Console.WriteLine("TMothd3 完成");56         return 3;57       });58     }59 60   }61 }

View Code

 




原标题:C# Async与Await用法

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

做Shopee,你要知道的流量机制和搜索机制!:https://www.goluckyvip.com/news/3296.html
百世上线美国陆运专线运力平台:https://www.goluckyvip.com/news/3297.html
美国已经通过TikTok禁令,卖家需有所准备:https://www.goluckyvip.com/news/3298.html
全球航空公司已倒闭23家,国际航空物流短板凸显:https://www.goluckyvip.com/news/3299.html
提升物流体验 淘宝四金冠店铺的仓储布局术:https://www.goluckyvip.com/news/33.html
还在为不了解货代英语发愁?货代英语知识汇总来啦(运费篇):https://www.goluckyvip.com/news/330.html
三百元以内千兆路由器怎么选择?:https://www.vstour.cn/a/363184.html
千岛湖绿城度假酒店的简介:https://www.vstour.cn/a/363185.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流