C#Async与Await用法

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

[flash=500,400]http://s1.knowsky.com/20160211/fsnj4ptdtgr56.jpg[/flash]

[flash=500,400]http://s1.knowsky.com/20160211/2w2i23bb1y256.gif[/flash][flash=500,400]http://s1.knowsky.com/20160211/i1ioyayvgqc56.gif[/flash]1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;5usingSystem.Threading;6usingSystem.Threading.Tasks;78namespaceCloudLearning.UI9{10classPRogram11{12staticvoidMain(string[] args)13{14AsyncPrintHelloWorld();15Console.ReadLine();16}1718publicasyncstaticvoidAsyncPrintHelloWorld()19{20Console.WriteLine("异步方法调用开始");21varresult1 =TMothd1();22varresult2 =TMothd2();23varresult3 =TMothd3();24Console.WriteLine("异步方法完成");25intr1 =awaitresult1;26intr2=awaitresult2;27intr3 =awaitresult3;28Console.WriteLine("{0},{1},{2}", r1, r2, r3);29}3031publicasyncstaticTask<int>TMothd1()32{33returnawaitTask.Factory.StartNew(() =>34{35Thread.Sleep(5*1000);36Console.WriteLine("TMothd1 完成");37return1;38});39}4041publicasyncstaticTask<int>TMothd2()42{43returnawaitTask.Factory.StartNew(() =>44{45Thread.Sleep(10*1000);46Console.WriteLine("TMothd2 完成");47return2;48});49}50publicasyncstaticTask<int>TMothd3()51{52returnawaitTask.Factory.StartNew(() =>53{54Thread.Sleep(15*1000);55Console.WriteLine("TMothd3 完成");56return3;57});58}5960}61}

View Code

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航