C # Asynchronous programming

Source: Internet
Author: User

Before asynchronous programming, give an example that is not asynchronous:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Diagnostics;//7 usingSystem.Net;//8 9 namespaceConsoleApplication6Ten { One     classmydownloadstring A     { -Stopwatch SW =NewStopwatch ();// -  the          Public voidDorun () -         { -             Const intLargenumber =6000000; - SW. Start (); +  -             intT1 = Countcharacters (1,"http://www.cnblogs.com"); +             intT2 = Countcharacters (2,"http://www.163.com"); A  atCounttoalargenumber (1, largenumber); -Counttoalargenumber (2, largenumber); -Counttoalargenumber (3, largenumber); -Counttoalargenumber (4, largenumber); -  -Console.WriteLine ("Chars in http://www.microsoft.com: {0}", T1); inConsole.WriteLine ("Chars in http://www.illustratedcsharp.com: {0}", T2); -         } to  +         Private intCountcharacters (intIdstringuristring) -         { theWebClient WC1 =NewWebClient (); *  $Console.WriteLine ("starting call {0}: {1, 4:n0} MS", ID, SW. Elapsed.totalmilliseconds);//start download page timePanax Notoginseng             stringresult = WC1. Downloadstring (NewUri (uristring));//official download page data -Console.WriteLine ("Call {0} completed: {1, 4:n0} MS", ID, SW. Elapsed.totalmilliseconds);//Complete Web Download the             //Console.WriteLine (result); +             returnresult. Length; A         } the  +         Private voidCounttoalargenumber (intIdintvalue) -         { $              for(Longi =0; I < value; i++) ; $  -Console.WriteLine ("End counting {0}: {1, 4:n0} MS", ID, SW. Elapsed.totalmilliseconds);//a loop end time, with the intention of delaying -  the         } -     }Wuyi     class Program the     { -         Static voidMain (string[] args) Wu         { -mydownloadstring ds =Newmydownloadstring (); About ds. Dorun (); $ Console.readkey (); -         } -  -     } A } + //Output: the //------------------------------------------------------------------------ - //starting call 1:1 ms $ //Call 1 completed:207 Ms the //starting call 2:207 Ms the //Call 2 completed:382 Ms the //End counting 1:424 Ms the //End counting 2:447 Ms - //End counting 3:469 Ms in //End counting 4:491 Ms the //Chars inhttp://www.microsoft.com: 41177 the //Chars inhttp://www.illustratedcsharp.com: 6870
View Code

Asynchronous

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Diagnostics;//usingSystem.Net;//namespaceconsoleapplication1{classmydownloadstring {Stopwatch SW=NewStopwatch ();  Public voidDorun () {Const intLargenumber =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 Asynctask<int> Countcharactersasync (intIdstringsite) {WebClient WC=NewWebClient (); Console.WriteLine ("String Call {0}: {1,4:n0} ms", ID, SW.            Elapsed.totalmilliseconds); stringRESULT1 =awaitWc. Downloadstringtaskasync (NewUri (site)); Console.WriteLine ("Call {0} completed: {1, 4:n0} MS", ID, SW.            Elapsed.totalmilliseconds); returnRESULT1.        Length; }        Private voidCounttoalargenumber (intIdintvalue) {             for(Longi =0; I < value; i++) ; Console.WriteLine ("End counting {0}: {1, 4:n0} MS", ID, SW.        Elapsed.totalmilliseconds); }    }    classProgram {Static voidMain (string[] args) {mydownloadstring DS=Newmydownloadstring (); 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 inhttp://www.164.com: 6870Call 2 completed:954 mschars inhttp://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 inhttp://www.microsoft.com: 6870Chars inhttp://www.illustratedcsharp.com: 599274*/

C # Asynchronous programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.