C # Async Method execution Code

Source: Internet
Author: User
Using System.Collections.Generic;  Using System.Linq;  Using System.Text;  Using System.Threading;      Using System.Runtime.Remoting.Messaging;      Namespace ConsoleApplication1 {public delegate int AddHandler (int a, int b); public class Addmethod {public static int Add (int a, int b) {Console.WriteLine ("Start calculation              : "+ A +" + "+ B); Thread.Sleep (3000); Simulate this method to run for three seconds Console.WriteLine ("Calculation done!              ");          return a + B; }}//************** synchronously calls the ***********//Delegate's Invoke method for synchronous invocation.          A synchronous call can also be called a blocking call, which blocks the current thread and then executes the call, and then continues down after the call is complete.      The asynchronous call to the ***********//asynchronous call does not block the thread, but instead plugs the call into the thread pool, which the program's main thread or UI thread can continue to execute.          The asynchronous invocation of a delegate is implemented by BeginInvoke and EndInvoke.          Asynchronous callback ***********//With callback function, the callback function is automatically invoked at the end of the call, which resolves the situation where the thread is still blocked waiting for the result to be called.      Note: BeginInvoke and EndInvoke must be called in pairs. Even if the return value is not required, EndInvoke must still be called, which may cause a memory leak. Class Program {static void Main (string[] args) {Console.WriteLine ("===== Synchronous call Syncinvoketest =====");              AddHandler handler = new AddHandler (addmethod.add); int Result=handler.              Invoke (); Console.WriteLine ("Keep doing something else ...                  ");              Console.WriteLine (result);                      Console.readkey ();              Console.WriteLine ("===== Asynchronous call Asyncinvoketest =====");              AddHandler handler1 = new AddHandler (addmethod.add); IAsyncResult Result1=handler1.              BeginInvoke (1,2,null,null); Console.WriteLine ("Keep doing something else ...                  "); The asynchronous Operation returns Console.WriteLine (Handler1.              EndInvoke (RESULT1));                  Console.readkey ();              Console.WriteLine ("===== Asynchronous callback Asyncinvoketest =====");              AddHandler handler2 = new AddHandler (addmethod.add); IAsyncResult result2 = Handler2.              BeginInvoke (1, 2, new AsyncCallback (Callback), null); Console.WriteLine ("Keep doing something else ...              "); Console.ReadKey (); Asynchronous delegate, you can also refer to the following wording://action action= (obj) =>method (obj); Action. BeginInvoke (obj,ar=>action.              EndInvoke (AR), null);          You can complete an operation in two simple sentences. } static void Callback (IAsyncResult result) {AddHandler handler = (AddHandler) (Asyncresul T) result).              AsyncDelegate; Console.WriteLine (handler.          EndInvoke (result)); }}} 
  • Related Article

    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.