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