In practice, for some long-time operations, we tend to encapsulate them as asynchronous calls to speed up the system response or improve the user experience, and here's an example:
There is a ready-made class MyMath, which has an Add method:
1 Public Class MyMath 2 {3 4 public int Add (int a, int b) 5 {6 System.Threading.Thread.Sleep (50 00); 7 return a + B; 8 } 9 }
The Add method is encapsulated, compared, and provides a "synchronous call" and "asynchronous call" two versions:
1///Asynchronous Call 2//</summary> 3//<param name= "a" ></param> 4//<PA Ram Name= "B" ></param> 5//<param name= "Callbackaction" ></param> 6//<RETURNS&G t;</returns> 7 static void Ayscadd (int a, int b, action<int> callbackaction) 8 {9 func<int> func = () =>10 {One return new MyMath (). Add (A, b);};13 func. BeginInvoke ((AR) =>14 {$ var result = Func. EndInvoke (AR); Callbackaction.invoke (result);},18 null); 19 20}21 22 <SUMMARY>23///</summary>25//<param name= "a" ></par AM>26//<param name= "B" ></param>27//<returns></returns>28 static I NT Syncadd (int a, int b) {+ Return nEW MyMath (). Add (A, b); 31}
Last Call Validation:
1 static void Main (string[] args) 2 {3 Console.WriteLine ("Synchronous call starts with"); 4 int a = Syncadd (1, 2); 5 Console.WriteLine ("Synchronous call Ends:" + a); 6 7 Console.WriteLine ("--------------------------"); 8 9 Console.WriteLine ("Start Asynchronous Call"); 10 Ayscadd (3, 4, (result) =>11 { Console.WriteLine ("Asynchronous Call Result:" + result) ; Console.WriteLine ("End of Asynchronous call"); console.readline ();
Full code:
1 using System; 2 3 Namespace Actiondemo 4 {5 class program 6 {7 static void Main (string[] args) 8 {9 Console.WriteLine ("Synchronous call starts with"), and the int a = Syncadd (1, 2); Console.WriteLine ("Synchronous call Ends:" + a); Console.WriteLine ("--------------------------"); Console.WriteLine ("Start Asynchronous Call"); 16 Ayscadd (3, 4, (Result) =>17 {Console.WriteLine ("Asynchronous Call Result:" + result); 19 }); Console.WriteLine ("End of Asynchronous call"); Console.ReadLine ();}24//<s UMMARY>26////</SUMMARY>28//<param name= "a" ></param>29 <param name= "B" ></param>30//<param name= "callbackaction" ></param>31//< ; returns></returns>32 static void Ayscadd (int a, int b, action<int> callbackaction) 33 {34 func<int> func = () =>35 {MyMath return new (). Add (A, b); Notoginseng};38 func. BeginInvoke ((AR) =>39 {var result = Func. EndInvoke (AR); Callbackaction.invoke (result),},43 null); 44 45}46 47 <SUMMARY>48///</SUMMARY>50//<param name= "a" ></par am>51//<param name= "B" ></param>52//<returns></returns>53 static I NT Syncadd (int a, int b), {Return to New MyMath (). Add (A, b);}57}58-public class MyMath60 {63-6 public int Add (int A, int.) 4 System.Threading.Thread.Sleep (b;66), return a +}67 68}69}
The output results are as follows:
Synchronous call Start = =
End of synchronous Call: 3
--------------------------
Asynchronous call Start = =
End of Asynchronous call
Asynchronous Call Result: 7
synchronous, asynchronous invocation of C#:func (RPM)