C # Delegate synchronous and asynchronous calls

Source: Internet
Author: User

The entrusted invoke method is used for Synchronous calling. Synchronous call can also be called blocking call. It will block the current thread and then execute the call. After the call is completed, continue to the next step.

Example of synchronous call:

using System;using System.Threading;public delegate int AddHandler(int a, int b);public class Foo {static void Main() {Console.WriteLine("**********SyncInvokeTest**************");AddHandler handler = new AddHandler(Add);int result = handler.Invoke(1,2);Console.WriteLine("Do other 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;}}

Running result:

* ********* Syncinvoketest **************

Computing 1 + 2...

Computing complete.

Do other work .........

3

 

Synchronous calling will block the thread. If it is to call a heavy job (such as a large number of Io operations), it may pause the program for a long time, resulting in poor
In this case, asynchronous calling is necessary.
Asynchronous calls do not block threads, but plug the calls into the thread pool. The main thread or UI thread of the program can continue to execute.
The asynchronous call of the delegate is implemented through begininvoke and endinvoke.
Asynchronous call:

using System;using System.Threading;public delegate int AddHandler(int a, int b);public class Foo {static void Main() {Console.WriteLine("**********AsyncInvokeTest**************");AddHandler handler = new AddHandler(Add);IAsyncResult result = handler.BeginInvoke(1,2,null,null);Console.WriteLine("Do other 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 other work .........
Computing 1 + 2...
Computing complete.
3

As you can see, the main thread does not wait, but runs down directly.
But the problem persists. When the main thread runs to endinvoke, if the call is not completed (this situation is likely to occur), the thread will still be blocked to wait for the call result.
The solution is to use a callback function. When the call ends, the callback function is automatically called.
Asynchronous callback:

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 other 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);}}

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.