最近在写程序的时候,经常遇到大量需要异步访问的情况,但是对于async和await到底怎么写,还不是非常明确。
1.普通的程序怎么写?
class Program { static void Main(string[] args) { MyDownLoadString ds = new MyDownLoadString(); ds.DoRun(); Console.ReadKey(); } class MyDownLoadString { Stopwatch sw = new Stopwatch(); public void DoRun() { const int LargeNumber = 6000000; sw.Start(); int t1 = CountCharacters(1, "http://www.microsoft.com"); int t2 = CountCharacters(2, "http://www.illustratedcsharp.com"); CountToALargeNumber(1, LargeNumber); CountToALargeNumber(2, LargeNumber); CountToALargeNumber(3, LargeNumber); CountToALargeNumber(4, LargeNumber); Console.WriteLine("Chars in Call1:{0}",t1); Console.WriteLine("Chars in Call1:{0}",t2); } private int CountCharacters(int id, string uriString) { WebClient wc1 = new WebClient(); Console.WriteLine("Call {0} start: {1:N0}ms ", id, sw.Elapsed.TotalMilliseconds); string result = wc1.DownloadString(new Uri(uriString)); Console.WriteLine("Call {0} completed: {1:N0}ms", id, sw.Elapsed.TotalMilliseconds); return result.Length; } private void CountToALargeNumber(int id, int value) { for (long i = 0; i < value; i++) ; Console.WriteLine("End CountToALargeNumber {0} : {1:N0}ms", id, sw.Elapsed.TotalMilliseconds); } } }
结果:
Call 1 start: 1ms
Call 1 completed: 903ms
Call 2 start: 903ms
Call 2 completed: 1,355ms
End CountToALargeNumber 1 : 1,375ms
End CountToALargeNumber 2 : 1,399ms
End CountToALargeNumber 3 : 1,417ms
End CountToALargeNumber 4 : 1,435ms
Chars in Call1:161702
Chars in Call1:5164
从运行结果可以看到,同步执行的时间主要花在了两次请求外部地址上,计算长度并不费时,用图来表示就像下面
2.使用async和await怎么写?
修改上面代码,如下
class MyDownLoadString { Stopwatch sw = new Stopwatch(); public void DoRun() { const int LargeNumber = 6000000; sw.Start(); // Task保存结果对象,后面t1.Result则是获取结果 Task t1 = CountCharactersAsync(1, "http://www.microsoft.com"); Task t2 = CountCharactersAsync(2, "http://www.illustratedcsharp.com"); //无需等待CountCharactersAsync执行完成 CountToALargeNumber(1, LargeNumber); CountToALargeNumber(2, LargeNumber); CountToALargeNumber(3, LargeNumber); CountToALargeNumber(4, LargeNumber); //t1.Result获取结果 Console.WriteLine("Chars in Call1:{0}",t1.Result); Console.WriteLine("Chars in Call1:{0}",t2.Result); } private async Task CountCharactersAsync(int id, string uriString) { WebClient wc = new WebClient(); Console.WriteLine("Call {0} start: {1:N0}ms ", id, sw.Elapsed.TotalMilliseconds); string result = await wc.DownloadStringTaskAsync(new Uri(uriString)); Trace.TraceInformation("Taceing Async Call {0} @time:{1:N0}ms", id, sw.Elapsed.TotalMilliseconds); Console.WriteLine("Call {0} completed: {1:N0}ms", id, sw.Elapsed.TotalMilliseconds); return result.Length; } private void CountToALargeNumber(int id, int value) { for (long i = 0; i < value; i++) ; Console.WriteLine("End CountToALargeNumber {0}: {1:N0}ms", id, sw.Elapsed.TotalMilliseconds); } }
运行结果:
Call 1 start: 2ms
Call 2 start: 253ms
End CountToALargeNumber 1: 288ms
End CountToALargeNumber 2: 359ms
End CountToALargeNumber 3: 560ms
Call 1 completed: 770ms
End CountToALargeNumber 4: 844ms
Call 2 completed: 887ms
Chars in Call1:162262
Chars in Call2:5164
修改如上面的代码之后,我们就可以无需等待两次CountCharactersAsync返回结果,而是直接调用了下面的CountToALargeNumber,在CountCharactersAsync请求返回的时候再获取结果。
3.async和await的细节
async和await可以创建和使用异步方法,这个特性的由三个部分组成:
- ①调用方法(calling method):该方法调用异步方法,然后在异步方法(可能使用同一个线程也可能不在一个线程)执行其任务的时候继续执行
- ②异步方法(async): 该方法异步执行其工作,然后立即方法到调用方法
- ③await表达式:用于异步方法内部,指明需要异步执行的惹怒我。一个异步方法可以包含任意多个await表达式,如果一个都不包含编译器会发出警告