[.NET] 使用 async & await 一步步將同步代碼轉換為非同步編程

來源:互聯網
上載者:User

標籤:using   sleep   sync   kth   如何   websites   object   cli   ref   

使用 async & await 一步步將同步代碼轉換為非同步編程

【博主】反骨仔    【出處】http://www.cnblogs.com/liqingwen/p/6079707.html   

  上次,博主通過《利用 async & await 的非同步編程》一文介紹了 async & await 的基本用法及非同步控制流程和一些其它的東西。

  今天,博主打算從建立一個普通的 WPF 應用程式開始,看看如何將它逐步轉換成一個非同步解決方案。你知道嗎?使用 Visual Studio 2012 的新特性可以更加容易、直觀的進行非同步編程。

 

介紹

  這裡通過一個普通的 WPF 程式進行講解:

  一個文字框和一個按鈕,左邊文字框的內容為點擊右鍵按鈕時所產生的結果。

 

添加引用

  並且加上 demo 要用到的 using 指令

using System.IO;using System.Net;using System.Net.Http;using System.Threading;

 

先建立一個同步的 WPF

  1.這是右邊點擊按鈕的事件:

 1         /// <summary> 2         /// 點擊事件 3         /// </summary> 4         /// <param name="sender"></param> 5         /// <param name="e"></param> 6         private void btnSwitch_Click(object sender, RoutedEventArgs e) 7         { 8             //清除文字框所有內容 9             tbResult.Clear();10 11             //統計總數12             SumSizes();13         }

  2.我在 SumSizes 方法內包含幾個方法:

    ① InitUrlInfoes:初始化 url 資訊列表;

    ② GetUrlContents:擷取網址內容;

    ③ DisplayResults:顯示結果。

 

  (1)SumSizes 方法:統計總數。

 1         /// <summary> 2         /// 統計總數 3         /// </summary> 4         private void SumSizes() 5         { 6             //載入網址 7             var urls = InitUrlInfoes(); 8  9             //位元組總數10             var totalCount = 0;11             foreach (var url in urls)12             {13                 //返回一個 url 內容的位元組數組14                 var contents = GetUrlContents(url);15 16                 //顯示結果17                 DisplayResults(url, contents);18 19                 //更新總數20                 totalCount += contents.Length;21             }22 23             tbResult.Text += $"\r\n         Total: {totalCount}, OK!";24         }

 

   (2)InitUrlInfoes 方法:初始化 url 資訊列表。

 1         /// <summary> 2         /// 初始化 url 資訊列表 3         /// </summary> 4         /// <returns></returns> 5         private IList<string> InitUrlInfoes() 6         { 7             var urls = new List<string>() 8             { 9                 "http://www.cnblogs.com/",10                 "http://www.cnblogs.com/liqingwen/",11                 "http://www.cnblogs.com/liqingwen/p/5902587.html",12                 "http://www.cnblogs.com/liqingwen/p/5922573.html"13             };14 15             return urls;16         }

 

  (3)GetUrlContents 方法:擷取網址內容。

 1         /// <summary> 2         /// 擷取網址內容 3         /// </summary> 4         /// <param name="url"></param> 5         /// <returns></returns> 6         private byte[] GetUrlContents(string url) 7         { 8             //假設下載速度平均延遲 300 毫秒 9             Thread.Sleep(300);10 11             using (var ms = new MemoryStream())12             {13                 var req = WebRequest.Create(url);14 15                 using (var response = req.GetResponse())16                 {17                     //從指定 url 裡讀取資料18                     using (var rs = response.GetResponseStream())19                     {20                         //從當前流中讀取位元組並將其寫入到另一流中21                         rs.CopyTo(ms);22                     }23                 }24 25                 return ms.ToArray();26             }27 28         }

 

  (4)DisplayResults 方法:顯示結果

 1         /// <summary> 2         /// 顯示結果 3         /// </summary> 4         /// <param name="url"></param> 5         /// <param name="content"></param> 6         private void DisplayResults(string url, byte[] content) 7         { 8             //內容長度 9             var bytes = content.Length;10 11             //移除 http:// 首碼12             var replaceUrl = url.Replace("http://", "");13 14             //顯示15             tbResult.Text += $"\r\n {replaceUrl}:   {bytes}";16         }

 

測試結果圖

 

 

[.NET] 使用 async & await 一步步將同步代碼轉換為非同步編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.