C # Multithreading BeginInvoke and EndInvoke methods for implementing asynchronous _ Call delegates with delegates

Source: Internet
Author: User

Each delegate in the 1.c# has a built-in BeginInvoke and EndInvoke method, and if there is only one method in the delegate's method list, the method can be executed asynchronously (not executed in the current thread, and another thread executes). The BeginInvoke and EndInvoke methods of entrustment are to be born for the above purposes.

2. The original thread initiated an asynchronous thread with the following three execution methods:

Way one: Wait until the original thread has started the asynchronous thread and has done some necessary processing, the original thread interrupts and waits for the asynchronous thread to finish before continuing.

Mode two: Polling, that is, the original thread periodically checks whether the originating thread is complete, and if not, can continue to do something else.

Method Three: Callback, that is, the original thread has been executed, without waiting or checking whether the originating thread is complete. At the end of the initiated thread execution, the originating thread invokes the user-defined callback method, which is used by the callback method to handle the results of the asynchronous method execution before calling EndInvoke.

3. A console applet, using the above three methods, the execution results are as follows:


4. Code:

Using system;using system.collections.generic;using system.linq;using system.runtime.remoting.messaging;using System.text;using System.threading;namespace uses delegates to implement asynchronous _ calls BeginInvoke and EndInvoke methods {delegate long Mydel (int first,int second); Declaring a delegate type class program {//Declaring a delegate type method static long Sum (int x,int y) {Console.writeli            NE ("Inside Sum");            Thread.Sleep (200);        return x + y;                    }//Defines the callback function to execute when the asynchronous thread executes the static void Callwhendone (IAsyncResult iar) {Console.WriteLine ("            Inside Callwhendone ");            AsyncResult ar = (AsyncResult) IAR; Mydel del = (Mydel) ar.            AsyncDelegate; Long result = Del.            EndInvoke (IAR);        Console.WriteLine ("The result is {0}.", result);            }//Mode one: Wait for the asynchronous thread to end, and then continue to execute the main thread static void Waituntildonestyle () {Mydel del = new Mydel (Sum); Console.WriteLine ("BeFore BeginInvoke "); IAsyncResult iar = Del. BeginInvoke (3, 5, NULL, NULL);            Begins an asynchronous call to Console.WriteLine ("After BeginInvoke");            Console.WriteLine ("Doing main stuff before"); Long result = Del. EndInvoke (IAR);            Waits for the asynchronous thread to end and gets the result Console.WriteLine ("After endinvoke:{0}", result);        Console.WriteLine ("Doing main stuff after"); }//Mode Two: Polling checks whether the asynchronous thread ends, and if not, executes the main thread static void Lunxunpollingstyle () {Mydel del = new Mydel (S            UM);            Console.WriteLine ("Before BeginInvoke"); IAsyncResult iar = Del. BeginInvoke (3, 5, NULL, NULL);            Begins an asynchronous call to Console.WriteLine ("After BeginInvoke"); while (!iar.                iscompleted) {Console.WriteLine ("Not done.doing main stuff");            Continue with the main thread thing for (long i = 0; i < 10000000; i++);            } Console.WriteLine ("Done"); Long result = Del. EndInvoke (IAR); Call EndInvoke to get the result and clean Console.WriteLine ("Result: {0}", result); }//Mode three: Callback mode, when the asynchronous thread ends, the system calls the user-defined method to process the result (including the EndInvoke method that invokes the delegate) static void Callbackstyle () {M            Ydel del = new Mydel (Sum);            Console.WriteLine ("Before BeginInvoke"); IAsyncResult iar = Del.            BeginInvoke (3, 5, New AsyncCallback (Callwhendone), null);            Console.WriteLine ("After BeginInvoke");            Console.WriteLine ("Doing more work in Main.");            Thread.Sleep (500); Console.WriteLine ("Done with Main.        Exiting. ");            static void Main (string[] args) {//Mode one: waits for the asynchronous thread to end before continuing to execute the main thread Console.WriteLine ();            Console.WriteLine ("--------mode one: Wait for the asynchronous thread to end, and then continue to execute the main thread--------");            Waituntildonestyle ();            Mode Two: Polling checks whether the asynchronous thread ends and executes the main thread Console.WriteLine () If it does not end;            Console.WriteLine ("--------mode two: Polling checks whether the asynchronous thread is over, and if it does not, executes the main thread--------");            Lunxunpollingstyle (); PartyType three: Callback mode, when the asynchronous thread ends, the system calls the user-defined method to process the result (including calling the delegate's EndInvoke method) Console.WriteLine ();            Console.WriteLine ("--------mode three: Callback mode, when the asynchronous thread ends, the system invokes a user-defined method to process the result (including invoking the EndInvoke method of the delegate)--------");        Callbackstyle (); }           }}


C # Multithreading BeginInvoke and EndInvoke methods for implementing asynchronous _ Call delegates with delegates

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.