Asynchronous delegate (APM) uses the Func asynchronous operation to handle time-consuming operations

Source: Internet
Author: User

Use delegates for asynchronous operations to handle time-consuming operations to prevent the main thread from blocking

Examples of Use:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceDemo7 {8     class Program9     {Ten  One         Static voidMain (string[] args) A         { -func<string> fun =Newfunc<string>(Test);  -Console.WriteLine ("Asynchronous execution Start (method of the asynchronous delegate):"+Fun . Method); theIAsyncResult asyncresult= fun. BeginInvoke (CallBack,New string[]{"Sadha","Sahsiuh"});//Start Asynchronous Operation -  -              for(inti =0; I <Ten; i++ ) -             { +Console.WriteLine ("Execute other code at the same time as the Async method executes:"+i); -             } +  AFun. EndInvoke (AsyncResult);//End Asynchronous at Console.readkey (); -         } -  -         /// <summary> -         ///methods for asynchronous execution -         /// </summary> in         /// <returns></returns> -          Public Static stringTest () to         { +             returnDateTime.Now.ToString (); -         } the  *         /// <summary> $         ///method for asynchronous method execution to complete the rollbackPanax Notoginseng         /// </summary> -         /// <param name= "Async" ></param> the          Public Static voidCallBack (IAsyncResultAsync) +         { A             string[] array=Async. AsyncState as string []; theConsole.WriteLine ("whether the asynchronous operation is complete:"+Async. iscompleted); +         } -     } $}

The above code runs the result:

Asynchronous delegate (APM) uses the Func asynchronous operation to handle time-consuming operations

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.