convert synchronous code to asynchronous programming using Async & await Step-by- step
"Bo Master" anti-bone Aberdeen "provenance" http://www.cnblogs.com/liqingwen/p/6079707.html
Order
last time, bloggers introduced the basic usage of async & await and the asynchronous control flow and some other things through asynchronous programming with Async & await.
Async solution . Do you know? The new features of Visual Studio 2012 make it easier and more intuitive to program asynchronously.
Introduction
This is explained by a common WPF program:
A text box and a button, the content of the left text box is the result of the right click button.
Add Reference
Plus the using command that the demo will use.
using System.IO; using System.Net; using System.Net.Http; using System.Threading;
First, create a synchronized WPF
1. This is the event of the right click button:
1 /// <summary>2 ///Click events3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 Private voidBtnswitch_click (Objectsender, RoutedEventArgs e)7 {8 //Clear all contents of text box9 tbresult.clear ();Ten One //Total Statistics A sumsizes (); -}
2. I include several methods within the Sumsizes method:
①initurlinfoes: Initializes a list of URL information;
②geturlcontents: Get the content of the website;
③displayresults: Displays the results.
(1) Sumsizes method: Total Statistics.
1 /// <summary>2 ///Total Statistics3 /// </summary>4 Private voidsumsizes ()5 {6 //Load URLs7 varURLs =initurlinfoes ();8 9 //Total BytesTen varTotalCount =0; One foreach(varUrlinchURLs) A { - //returns a byte array of the contents of a URL - varContents =geturlcontents (URL); the - //Show Results - displayresults (URL, contents); - + //Total Updates -TotalCount + =contents. Length; + } A atTbresult.text + = $"\ r \ n total: {totalcount}, ok! "; -}
(2) Initurlinfoes method: Initializes a list of URL information.
1 /// <summary>2 ///initializing a list of URL information3 /// </summary>4 /// <returns></returns>5 Privateilist<string>initurlinfoes ()6 {7 varURLs =Newlist<string>()8 {9 "http://www.cnblogs.com/",Ten "http://www.cnblogs.com/liqingwen/", One "http://www.cnblogs.com/liqingwen/p/5902587.html", A "http://www.cnblogs.com/liqingwen/p/5922573.html" - }; - the returnURLs; -}
(3) Geturlcontents method: Get URL content.
1 /// <summary>2 ///Get URL content3 /// </summary>4 /// <param name= "url" ></param>5 /// <returns></returns>6 Private byte[] Geturlcontents (stringURL)7 {8 //assuming an average download speed delay of 300 milliseconds9Thread.Sleep ( -);Ten One using(varms =NewMemoryStream ()) A { - varreq =webrequest.create (URL); - the using(varResponse =req. GetResponse ()) - { - //reads data from the specified URL - using(varrs =Response. GetResponseStream ()) + { - //reads a byte from the current stream and writes it to another class + Rs. CopyTo (MS); A } at } - - returnMs. ToArray (); - } - -}
(4) DisplayResults method: Show Results
1 /// <summary>2 ///Show Results3 /// </summary>4 /// <param name= "url" ></param>5 /// <param name= "content" ></param>6 Private voidDisplayResults (stringUrlbyte[] content)7 {8 //Content Length9 varbytes =content. Length;Ten One //To remove http://prefix A varReplaceurl = URL. Replace ("/ http",""); - - //Show theTbresult.text + = $"\ r \ n {replaceurl}: {bytes}"; -}
Test result diagram
[. NET] Using async & await to convert synchronous code to asynchronous programming