. NET Async await

Source: Internet
Author: User

usingSystem;usingSystem.Net.Http;usingSystem.Threading;usingSystem.Threading.Tasks;namespacecookbook{classProgram {Static voidMain (string[] args) {            //Class1 class1 = new Class1 (); //Console.WriteLine ("Main thread start"); //Class1.            Dosomethingasync (); //Console.WriteLine ("Main thread End"); //Class2 class2 = new Class2 (); //Console.WriteLine ("Main thread start"); //Class2.            Dosomethingasync (); //Console.WriteLine ("Main thread End"); //String info = Class3.geturlstr ("http://www.baidu.com"); //Console.WriteLine (info);Console.WriteLine ("1 Main thread start: {0}", Thread.CurrentThread.ManagedThreadId); Program P=NewProgram (); stringURL ="http://www.baidu.com"; intTimeout =1;            P.dosomethingasync (URL, timeout);            P.dosomething (); Console.WriteLine ("6 Main thread end: {0}", Thread.CurrentThread.ManagedThreadId);        Console.readkey (); }         Public AsyncTask Dosomethingasync (stringUrlintTimeout) {Console.WriteLine ("2 Child thread start: {0}", Thread.CurrentThread.ManagedThreadId); Try            {                varresult =awaitdownloadstringwithretries (URL, timeout); Console.WriteLine ("{0}", result); }            Catch(Exception ex) {Console.WriteLine ("7 error: {1},{0}", Thread.CurrentThread.ManagedThreadId, ex.            Message); } Console.WriteLine ("4 Child thread end: {0}", Thread.CurrentThread.ManagedThreadId); }        Private voiddosomething () {Console.WriteLine ("3 main thread start: {0}", Thread.CurrentThread.ManagedThreadId);  for(intj =0; J! = +; J + +)            {                intresult =0;  for(inti =0; I! =10000000; i++) {result*=i; }} Console.WriteLine ("5 Main thread start: {0}", Thread.CurrentThread.ManagedThreadId); }        /// <summary>        ///get URL source code, error retry/// </summary>        /// <param name= "url" ></param>        /// <param name= "Timeout" ></param>        /// <returns></returns>        Private Asynctask<string> Downloadstringwithretries (stringUrlintTimeout) {            varNextdelay = Timespan.fromseconds (1);  for(inti =0; I! =3; ++i) {Try                {                    return awaitdownloadstringwithtimeout (URL, timeout); }                Catch                {                }                awaitTask.delay (Nextdelay); Nextdelay= Nextdelay +Nextdelay; }            return awaitdownloadstringwithtimeout (URL, timeout); }        /// <summary>        ///get URL source code, time out direct error/// </summary>        /// <param name= "url" ></param>        /// <param name= "Timeout" ></param>        /// <returns></returns>        Private Asynctask<string> Downloadstringwithtimeout (stringUrlintTimeout) {            using(varClient =NewHttpClient ()) {                varDownloadtask =client.                Getstringasync (URL); varTimeouttask =Task.delay (timeout); varCompletedtask =awaitTask.whenany (Downloadtask, Timeouttask); if(Completedtask = =timeouttask) {                    Throw NewException ("Processing Timeout"); }                return awaitDownloadtask; }        }    }}

. NET Async await

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.