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