C # Delegate asynchronous call

Source: Internet
Author: User

This article will mainly explain the differences and advantages and disadvantages of using the "synchronous call", "Asynchronous call", and "Asynchronous callback" to execute the same "addition class" with a delegate.
 

First, use the code to define a delegate and the methods to be called in the following three examples:

/* Added namespace
Using System. Threading;
Using System. Runtime. Remoting. Messaging;
*/
Public delegate int AddHandler (int a, int B );
Public class addition class
{
Public static int Add (int a, int B)
{
Console. WriteLine ("START calculation:" + a + "+" + B );
Thread. Sleep (3000); // simulate this method for three seconds
Console. WriteLine ("computing completed! ");
Return a + B;
}
}

 

 

Synchronous call

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.

Synchronous call of public class
{
Static void Main ()
{
Console. WriteLine ("===== synchronously call SyncInvokeTest ==== ");
AddHandler handler = new AddHandler (addition class. Add );
Int result = handler. Invoke (1, 2 );

Console. WriteLine ("continue to do other things... ");

Console. WriteLine (result );
& Nbs

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.