C # (synchronous call, asynchronous call, asynchronous callback)

Source: Internet
Author: User

[Csharp]
<SPAN style = "FONT-SIZE: 24px"> using System;
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 for three seconds
Console. WriteLine ("computing completed! ");
Return a + B;
}
}


***********
// 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.
 
// *************** Asynchronous call ***********
// Asynchronous calling does not block the thread, but inserts the call into the thread pool. The main thread or UI thread of the program can continue to execute the call.
// The asynchronous call of the delegate is implemented through BeginInvoke and EndInvoke.
 
// *************** Asynchronous callback ***********
// When a callback function is used, the callback function is automatically called at the end of the call. This solves the situation where the thread is still blocked to wait for the call result.
 
// Note: BeginInvoke and EndInvoke must be called in pairs. Even if no return value is required, EndInvoke must be called; otherwise, memory leakage may occur.
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("===== synchronously call SyncInvokeTest ==== ");
AddHandler handler = new AddHandler (AddMethod. Add );
Int result = handler. Invoke (1, 2 );
Console. WriteLine ("continue to do other things... ");
 
Console. WriteLine (result );
Console. ReadKey ();
 
 
Console. WriteLine ("===== asynchronously calling AsyncInvokeTest ==== ");
AddHandler handler1 = new AddHandler (AddMethod. Add );
IAsyncResult result1 = handler1.BeginInvoke (1, 2, null, null );
Console. WriteLine ("continue to do other things... ");
 
// Return an Asynchronous Operation
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 ("continue to do other things... ");
Console. ReadKey ();
 
 
// Asynchronous delegation, which can also be written as follows:
// Action <object> action = (obj) => method (obj );
// Action. BeginInvoke (obj, ar => action. EndInvoke (ar), null );
// An operation can be completed simply by two sentences.
}
Static void Callback (IAsyncResult result)
{
AddHandler handler = (AddHandler) (AsyncResult) result). AsyncDelegate;
Console. WriteLine (handler. EndInvoke (result ));
}
}
}
</SPAN>

Using System;
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 for three seconds
Console. WriteLine ("computing completed! ");
Return a + B;
}
}


***********
// 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.

// *************** Asynchronous call ***********
// Asynchronous calling does not block the thread, but inserts the call into the thread pool. The main thread or UI thread of the program can continue to execute the call.
// The asynchronous call of the delegate is implemented through BeginInvoke and EndInvoke.

// *************** Asynchronous callback ***********
// When a callback function is used, the callback function is automatically called at the end of the call. This solves the situation where the thread is still blocked to wait for the call result.

// Note: BeginInvoke and EndInvoke must be called in pairs. Even if no return value is required, EndInvoke must be called; otherwise, memory leakage may occur.
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("===== synchronously call SyncInvokeTest ==== ");
AddHandler handler = new AddHandler (AddMethod. Add );
Int result = handler. Invoke (1, 2 );
Console. WriteLine ("continue to do other things... ");

Console. WriteLine (result );
Console. ReadKey ();


Console. WriteLine ("===== asynchronously calling AsyncInvokeTest ==== ");
AddHandler handler1 = new AddHandler (AddMethod. Add );
IAsyncResult result1 = handler1.BeginInvoke (1, 2, null, null );
Console. WriteLine ("continue to do other things... ");

// Return an Asynchronous Operation
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 ("continue to do other things... ");
Console. ReadKey ();


// Asynchronous delegation, which can also be written as follows:
// Action <object> action = (obj) => method (obj );
// Action. BeginInvoke (obj, ar => action. EndInvoke (ar), null );
// An operation can be completed simply by two sentences.
}
Static void Callback (IAsyncResult result)
{
AddHandler handler = (AddHandler) (AsyncResult) 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.