转于:https://www.cnblogs.com/liqingwen/p/5831951.html
class Program { //创建计时器 private static readonly Stopwatch Watch = new Stopwatch(); private static void Main(string[] args) { //启动计时器 Watch.Start(); const string url1 = "http://www.cnblogs.com/"; const string url2 = "http://www.cnblogs.com/liqingwen/"; //两次调用 CountCharacters 方法(下载某网站内容,并统计字符的个数) var result1 = CountCharacters(1, url1); var result2 = CountCharacters(2, url2); //三次调用 ExtraOperation 方法(主要是通过拼接字符串达到耗时操作) for (var i = 0; i < 3; i++) { ExtraOperation(i + 1); } //控制台输出 Console.WriteLine($"{url1} 的字符个数:{result1}"); Console.WriteLine($"{url2} 的字符个数:{result2}"); Console.Read(); } /// <summary> /// 统计字符个数 /// </summary> /// <param name="id"></param> /// <param name="address"></param> /// <returns></returns> private static int CountCharacters(int id, string address) { var wc = new WebClient(); Console.WriteLine($"开始调用 id = {id}:{Watch.ElapsedMilliseconds} ms 线程ID:{Thread.CurrentThread.ManagedThreadId}"); var result = wc.DownloadString(address); Console.WriteLine($"调用完成 id = {id}:{Watch.ElapsedMilliseconds} ms 线程ID:{Thread.CurrentThread.ManagedThreadId}"); return result.Length; } /// <summary> /// 额外操作 /// </summary> /// <param name="id"></param> private static void ExtraOperation(int id) { //这里是通过拼接字符串进行一些相对耗时的操作 var s = ""; for (var i = 0; i < 6000; i++) { s += i; } Console.WriteLine($"id = {id} 的 ExtraOperation 方法完成:{Watch.ElapsedMilliseconds} ms"); } }
这段代码按照顺序执行后的效果:
如果想提高执行的效率,那我们可以使用C# 的 async/await:
1 class Program 2 { 3 //创建计时器 4 private static readonly Stopwatch Watch = new Stopwatch(); 5 6 private static void Main(string[] args) 7 { 8 //启动计时器 9 Watch.Start(); 10 11 const string url1 = "http://www.cnblogs.com/"; 12 const string url2 = "http://www.cnblogs.com/liqingwen/"; 13 14 //两次调用 CountCharactersAsync 方法(异步下载某网站内容,并统计字符的个数) 15 Task<int> t1 = CountCharactersAsync(1, url1); 16 Task<int> t2 = CountCharactersAsync(2, url2); 17 18 //三次调用 ExtraOperation 方法(主要是通过拼接字符串达到耗时操作) 19 for (var i = 0; i < 3; i++) 20 { 21 ExtraOperation(i + 1); 22 } 23 24 //控制台输出 25 Console.WriteLine($"{url1} 的字符个数:{t1.Result}"); 26 Console.WriteLine($"{url2} 的字符个数:{t2.Result}"); 27 28 Console.Read(); 29 } 30 31 /// <summary> 32 /// 统计字符个数 33 /// </summary> 34 /// <param name="id"></param> 35 /// <param name="address"></param> 36 /// <returns></returns> 37 private static async Task<int> CountCharactersAsync(int id, string address) 38 { 39 var wc = new WebClient(); 40 Console.WriteLine($"开始调用 id = {id}:{Watch.ElapsedMilliseconds} ms 线程ID:{Thread.CurrentThread.ManagedThreadId}"); 41 42 var result = await wc.DownloadStringTaskAsync(address); 43 Console.WriteLine($"调用完成 id = {id}:{Watch.ElapsedMilliseconds} ms 线程ID:{Thread.CurrentThread.ManagedThreadId}"); 44 45 return result.Length; 46 } 47 48 /// <summary> 49 /// 额外操作 50 /// </summary> 51 /// <param name="id"></param> 52 private static void ExtraOperation(int id) 53 { 54 //这里是通过拼接字符串进行一些相对耗时的操作 55 var s = ""; 56 57 for (var i = 0; i < 6000; i++) 58 { 59 s += i; 60 } 61 62 Console.WriteLine($"id = {id} 的 ExtraOperation 方法完成:{Watch.ElapsedMilliseconds} ms"); 63 } 64 }
这是使用C# 的 async/await完善后的输出,从输出信息上看出执行await代码前后线程的ID发生了变化,新建线程15和19执行CountCharactersAsync统计函数;同时执行时间比同步执行有不少的提高:
①从 Main 方法执行到 CountCharactersAsync(1, url1) 方法时,该方法会立即返回,然后才会调用它内部的方法开始下载内容。该方法返回的是一个 Task<int> 类型的占位符对象,表示计划进行的工作。这个占位符最终会返回 int 类型的值。
②这样就可以不必等 CountCharactersAsync(1, url1) 方法执行完成就可以继续进行下一步操作。到执行 CountCharactersAsync(2, url2) 方法时,跟 ① 一样返回 Task<int> 对象。
③然后,Main 方法继续执行三次 ExtraOperation 方法,同时两次 CountCharactersAsync 方法依然在持续工作 。
④t1.Result 和 t2.Result 是指从 CountCharactersAsync 方法调用的 Task<int> 对象取结果,如果还没有结果的话,将阻塞,直有结果返回为止。
异步方法:在Winform窗口应用程序中执行异步方法。
public partial class Form1 : Form { private static readonly Stopwatch Watch = new Stopwatch(); private const string url1 = "http://www.cnblogs.com/"; private const string url2 = "http://www.cnblogs.com/liqingwen/"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Console.WriteLine("button 1:" + Thread.CurrentThread.ManagedThreadId); Thread.Sleep(5000); button1.Text = "同步方法调用"; Console.WriteLine("button 1:" + Thread.CurrentThread.ManagedThreadId); } private async void button2_Click(object sender, EventArgs e) { Console.WriteLine("button 2:" + Thread.CurrentThread.ManagedThreadId); Watch.Start(); var delay = ThreadSleep(1, url1); button2.Text = await delay; Console.WriteLine("button 2:" + Thread.CurrentThread.ManagedThreadId); } private static async Task<string> ThreadSleep(int id) { await Task.Run<string>(() => { Console.WriteLine("线程ID1:" + Thread.CurrentThread.ManagedThreadId); Thread.Sleep(5000); Console.WriteLine("线程ID2:" + Thread.CurrentThread.ManagedThreadId); return "10"; }); Console.WriteLine("线程ID3:" + Thread.CurrentThread.ManagedThreadId); return "11"; } private Task<string> ThreadSleep(int id,string msg) { return Task.Run<string>(() => { Console.WriteLine("button 2:" + Thread.CurrentThread.ManagedThreadId); Thread.Sleep(5000); return "异步方法调用"; }); } } }
从输出窗口中看出await中线程ID发生了变化,await前后线程ID还是在主线程中执行