synchronous, asynchronous invocation of C#:func (RPM)

Source: Internet
Author: User

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//&LT;PA Ram Name= "B" ></param> 5//<param name= "Callbackaction" ></param> 6//&LT;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 &LT;SUMMARY&GT;23///</summary>25//<param name= "a" ></par AM&GT;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&GT;26////&LT;/SUMMARY&GT;28//<param name= "a" ></param>29 <param name= "B" ></param>30//<param name= "callbackaction" ></param>31//&lt         ; 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 &LT;SUMMARY&GT;48///&LT;/SUMMARY&GT;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)

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.