First, define a delegate and the following three examples to call through code:
public delegate int AddHandler(int a,int b);
public class addition class
{
public static int Add(int a, int b)
{
("Start calculation:" + a + "+" + b);
(3000); //Simulate this method and run for three seconds
("Computation is completed!");
return a + b;
}
}
Synchronous call
The delegated Invoke method is used to make synchronous calls. A synchronous call can also be called a blocking call. It will block the current thread, then execute the call, and continue to proceed downward after the call is completed.
Public class synchronous call
{
static void Main()
{
("====== Synchronous call SyncInvokeTest ======");
AddHandler handler = new AddHandler(add class.Add);
int result = (1, 2);
("Continue to do something else...");
(result);
();
}
}
Synchronous calls will block threads. If you want to call a heavy task (such as a large number of IO operations), it may cause the program to pause for a long time and cause a bad user experience. At this time, asynchronous calls are necessary.
Asynchronous call
Asynchronous calls do not block threads, but instead stuff the calls into the thread pool, and the main thread of the program or UI thread can continue to execute. The delegated asynchronous calls are implemented through BeginInvoke and EndInvoke.
public class asynchronous call
{
static void Main()
{
("====== Asynchronous call AsyncInvokeTest ======");
AddHandler handler = new AddHandler(add class.Add);
//IAsyncResult: Asynchronous operation interface (interface)
//BeginInvoke: The beginning of an asynchronous method of delegate
IAsyncResult result = (1, 2, null, null);
("Continue to do something else...");
//Asynchronous operation returns
((result));
();
}
}
As you can see, the main thread did not wait, but ran directly downward. But the problem still exists. When the main thread runs to EndInvoke, if the call does not end at this time (this case is likely to occur), the thread will still be blocked in order to wait for the call result.
Asynchronous delegation, you can also refer to the following writing method:
Action<object> action=(obj)=>method(obj);
(obj,ar=>(ar),null);
One operation can be completed in just two simple sentences.
Asynchronous callback
Use the callback function, and the callback function will be automatically called when the call ends, solving the situation where the thread is still blocked in order to wait for the call result.
public class asynchronous callback
{
static void Main()
{
("====== Asynchronous callback AsyncInvokeTest ======");
AddHandler handler = new AddHandler(add class.Add);
//Asynchronous operation interface (note the difference in BeginInvoke method!)
IAsyncResult result = (1,2,new AsyncCallback(callback function),"AsyncState:OK");
("Continue to do something else...");
();
}
static void callback function (IAsyncResult result)
{
//result is the return value of "Add class.Add() method"
//AsyncResult is an implementation class of the IAsyncResult interface, space:
//The AsyncDelegate property can be cast to the actual class of the user-defined delegate.
AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
((result));
();
}
}
The type of delegate I defined is AddHandler, then in order to access , the asynchronous delegate must be cast to AddHandler. You can call it in an asynchronous callback function (type AsyncCallback) to get the result of the initially submitted.
question:
(1)int result = (1,2);
Why are the parameters and return values of Invoke the same as the AddHandler delegation?
Answer: The parameters of the Invoke method are very simple, one delegate and one parameter table (optional). The main function of the Invoke method is to help you call the method specified by the delegate on the UI thread. The Invoke method first checks whether the thread that issued the call (i.e. the current thread) is a UI thread. If yes, directly execute the delegate-pointed method. If not, it will switch to the UI thread and then execute the delegate-pointed method. Regardless of whether the current thread is a UI thread or not, Invoke blocks until the delegate-pointed method is executed, and then switches back to the thread that issued the call (if needed) and returns.
Therefore, the parameters and return values of the Invoke method should be consistent with the delegate called.
(2)IAsyncResult result = (1,2,null,null);
BeginInvoke: Start an asynchronous request and call a thread in the thread pool to execute.
Returns the IAsyncResult object (the core of asynchronous). In short, IAsyncResult is an interface that stores the status information of asynchronous operations, and can also be used to end the current asynchronous operation.
Note: BeginInvoke and EndInvoke must be called in pairs. Even if the return value is not required, EndInvoke must still be called, otherwise it may cause a memory leak.
(3) Attributes:
Gets a user-defined object that qualifies or contains information about asynchronous operations. For example:
static void AddComplete(IAsyncResult result)
{
AddHandler handler = (AddHandler);
((result));
}
The complete code is as follows:
using System;
using ;
using ;
using ;
using ;
using ;
namespace ConsoleTest
{
public delegate int AddHandler(int a,int b);
public class addition class
{
public static int Add(int a, int b)
{
("Start calculation:" + a + "+" + b);
(3000); //Simulate this method and run for three seconds
("Computation is completed!");
return a + b;
}
}
Public class synchronous call
{
static void Main()
{
("====== Synchronous call SyncInvokeTest ======");
AddHandler handler = new AddHandler(add class.Add);
int result = (1, 2);
("Continue to do something else...");
(result);
();
}
}
public class asynchronous call
{
static void Main()
{
("====== Asynchronous call AsyncInvokeTest ======");
AddHandler handler = new AddHandler(add class.Add);
//IAsyncResult: Asynchronous operation interface (interface)
//BeginInvoke: The beginning of an asynchronous method of delegate
IAsyncResult result = (1, 2, null, null);
("Continue to do something else...");
//Asynchronous operation returns
((result));
();
}
}
public class asynchronous callback
{
static void Main()
{
("====== Asynchronous callback AsyncInvokeTest ======");
AddHandler handler = new AddHandler(add class.Add);
//Asynchronous operation interface (note the difference in BeginInvoke method!)
IAsyncResult result = (1,2,new AsyncCallback(callback function),"AsyncState:OK");
("Continue to do something else...");
();
}
static void callback function (IAsyncResult result)
{
//result is the return value of "Add class.Add() method"
//AsyncResult is an implementation class of the IAsyncResult interface, with reference space:
//The AsyncDelegate property can be cast to the actual class of the user-defined delegate.
AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
((result));
();
}
}
}