Synchronous invocation and asynchronous invocation of C # delegates

Source: Internet
Author: User

The Invoke method of the delegate is used 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.

Examples of synchronous calls:

Using system;using system.threading;public delegate int AddHandler (int a, int b);p Ublic class Foo {static void Main () {Con Sole. WriteLine ("**********syncinvoketest**************"); AddHandler handler = new AddHandler (ADD); int result = handler. Invoke (); Console.WriteLine ("Do the work ..."); Console.WriteLine (result); Console.ReadLine ();} static int Add (int a, int b) {Console.WriteLine ("Computing" +a+ "+" +b+ "..."); Thread.Sleep (3000); Console.WriteLine ("Computing complete."); return a+b;}}

Operation Result:

syncinvoketest**************

Computing 1 + 2 ...

Computing complete.

Do and work .....

3

A synchronous call blocks the thread, and if it is to invoke a heavy workload (such as a large number of IO operations), it may cause the program to stall for a long time, causing bad
User experience, it is necessary to call it asynchronously.
Instead of blocking the thread, the asynchronous call 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 invocation:

Using system;using system.threading;public delegate int AddHandler (int a, int b);p Ublic class Foo {static void Main () {Con Sole. WriteLine ("**********asyncinvoketest**************"); AddHandler handler = new AddHandler (ADD); IAsyncResult result = handler. BeginInvoke (1,2,null,null); Console.WriteLine ("Do the work ..."); Console.WriteLine (handler. EndInvoke (result)); Console.ReadLine ();} static int Add (int a, int b) {Console.WriteLine ("Computing" +a+ "+" +b+ "..."); Thread.Sleep (3000); Console.WriteLine ("Computing complete."); return a+b;}}

Running Result: **********asyncinvoketest**************
Do and work .....
Computing 1 + 2 ...
Computing complete.
3


as you can see, the main thread does not wait, but it runs down directly.
But the problem persists, when the main thread runs to EndInvoke, if the call is not ended (which is likely to occur), the thread will still be blocked in order to wait for the result to be called.

callback async:

public class Foo {static void Main () {Console.WriteLine ("**********asyncinvoketest**************"); AddHandler handler = new AddHandler (ADD); IAsyncResult result = handler. BeginInvoke (1,2,new AsyncCallback (Addcomplete), "Asycstate:ok"); Console.WriteLine ("Do the work ..."); Console.ReadLine ();} static int Add (int a, int b) {Console.WriteLine ("Computing" +a+ "+" +b+ "..."); Thread.Sleep (3000); Console.WriteLine ("Computing complete."); return a+b;} static void Addcomplete (IAsyncResult result) {AddHandler handler = (AddHandler) ((AsyncResult) result). AsyncDelegate; Console.WriteLine (handler. EndInvoke (result)); Console.WriteLine (Result. asyncstate);}}

Synchronous invocation and asynchronous invocation of C # 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.