在异步编程前,先举个没有异步的示例:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Diagnostics;// 7 using System.Net;// 8 9 namespace ConsoleApplication610 {11 class MyDownloadString12 {13 Stopwatch sw = new Stopwatch();//14 15 public void DoRun()16 {17 const int LargeNumber = 6000000;18 sw.Start();19 20 int t1 = CountCharacters(1, "http://www.cnblogs.com");21 int t2 = CountCharacters(2, "http://www.163.com");22 23 CountToALargeNumber(1, LargeNumber);24 CountToALargeNumber(2, LargeNumber);25 CountToALargeNumber(3, LargeNumber);26 CountToALargeNumber(4, LargeNumber);27 28 Console.WriteLine("Chars in http://www.microsoft.com :{0}", t1);29 Console.WriteLine("Chars in http://www.illustratedcsharp.com :{0}", t2);30 }31 32 private int CountCharacters(int id, string uriString)33 {34 WebClient wc1 = new WebClient();35 36 Console.WriteLine("Starting call {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);//开始下载网页时间37 string result = wc1.DownloadString(new Uri(uriString));//正式下载网页数据38 Console.WriteLine("Call {0} completed : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);//完成网页下载39 //Console.WriteLine(result);40 return result.Length;41 }42 43 private void CountToALargeNumber(int id, int value)44 {45 for (long i = 0; i < value; i++) ;46 47 Console.WriteLine("End counting {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);//一个循环结束时间,目的是延时48 49 }50 }51 class Program52 {53 static void Main(string[] args)54 {55 MyDownloadString ds = new MyDownloadString();56 ds.DoRun();57 Console.ReadKey();58 }59 60 }61 }62 //output:63 //------------------------------------------------------------------------64 //Starting call 1 : 1 ms65 //Call 1 completed: 207 ms66 //Starting call 2 : 207 ms67 //Call 2 completed: 382 ms68 //End counting 1 : 424 ms69 //End counting 2 : 447 ms70 //End counting 3 : 469 ms71 //End counting 4 : 491 ms72 //Chars in http://www.microsoft.com :4117773 //Chars in http://www.illustratedcsharp.com :6870
异步
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Diagnostics;//using System.Net;//namespace ConsoleApplication1{ class MyDownloadString { Stopwatch sw = new Stopwatch(); public void DoRun() { const int LargeNumber = 6000000; sw.Start(); Task t1 = CountCharactersAsync(1, "http://www.163.com"); Task t2 = CountCharactersAsync(2, "http://www.qq.com"); CountToALargeNumber(1, LargeNumber); CountToALargeNumber(2, LargeNumber); CountToALargeNumber(3, LargeNumber); CountToALargeNumber(4, LargeNumber); Console.WriteLine("Chars in http://www.164.com : {0}", t1.Result); Console.WriteLine("Chars in http://www.qq.com : {0}", t2.Result); } private async Task CountCharactersAsync(int id, string site) { WebClient wc = new WebClient(); Console.WriteLine("String call {0} : {1,4:N0} ms", id, sw.Elapsed.TotalMilliseconds); string result1 = await wc.DownloadStringTaskAsync(new Uri(site)); Console.WriteLine("Call {0} completed : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds); return result1.Length; } private void CountToALargeNumber(int id, int value) { for (long i = 0; i < value; i++) ; Console.WriteLine("End counting {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds); } } class Program { static void Main(string[] args) { MyDownloadString ds = new MyDownloadString(); ds.DoRun(); Console.ReadKey(); } }}/*String call 1 : 2 msString call 2 : 160 msEnd counting 1 : 261 msEnd counting 2 : 283 msEnd counting 3 : 309 msEnd counting 4 : 334 msCall 1 completed : 353 msChars in http://www.164.com : 6870Call 2 completed : 954 msChars in http://www.qq.com : 599258---------------------------------------------------------------------Starting call 1 : 1 msCall 1 completed : 255 msStarting call 2 : 255 msCall 2 completed : 751 msEnd counting 1 : 785 msEnd counting 2 : 808 msEnd counting 3 : 830 msEnd counting 4 : 852 msChars in http://www.microsoft.com :6870Chars in http://www.illustratedcsharp.com :599274*/