标签:
在异步编程前,先举个没有异步的示例:
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 ConsoleApplication6 10 { 11 class MyDownloadString 12 { 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 Program 52 { 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 ms 65 //Call 1 completed: 207 ms 66 //Starting call 2 : 207 ms 67 //Call 2 completed: 382 ms 68 //End counting 1 : 424 ms 69 //End counting 2 : 447 ms 70 //End counting 3 : 469 ms 71 //End counting 4 : 491 ms 72 //Chars in http://www.microsoft.com :41177 73 //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<int> t1 = CountCharactersAsync(1, "http://www.163.com"); Task<int> 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<int> 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 ms String call 2 : 160 ms End counting 1 : 261 ms End counting 2 : 283 ms End counting 3 : 309 ms End counting 4 : 334 ms Call 1 completed : 353 ms Chars in http://www.164.com : 6870 Call 2 completed : 954 ms Chars in http://www.qq.com : 599258 --------------------------------------------------------------------- Starting call 1 : 1 ms Call 1 completed : 255 ms Starting call 2 : 255 ms Call 2 completed : 751 ms End counting 1 : 785 ms End counting 2 : 808 ms End counting 3 : 830 ms End counting 4 : 852 ms Chars in http://www.microsoft.com :6870 Chars in http://www.illustratedcsharp.com :599274 */
标签:
原文地址:http://www.cnblogs.com/wanghaibin/p/4937373.html