For details about the three delegate methods, refer to this article.
Http://www.cnblogs.com/aierong/archive/2005/05/25/162181.html
Let's first understand the two methods and a delegate and an interface:
(1)
The BeginInvoke method can be used to initiate asynchronous calls.
It has the same parameters as the method to be executed asynchronously. It also has two optional parameters. The first parameter is an AsyncCallback delegate that references the method to be called when the asynchronous call is complete. The second parameter is a user-defined object that can pass information to the callback method. BeginInvoke returns immediately without waiting for the asynchronous call to complete. BeginInvoke returns IAsyncResult, which can be used to monitor the asynchronous call progress.
The result object IAsyncResult is returned from the start operation and can be used to obtain the status of whether the Asynchronous Start operation is complete.
The result object is passed to the end operation, which returns the final return value of the call.
You can provide an optional callback when you start the operation. If a callback is provided, the callback is called after the call is completed, and the code in the callback can call the end operation.
(2)
The EndInvoke method retrieves the result of an asynchronous call.
After BeginInvoke is called, you can call the EndInvoke method at any time. If the asynchronous call is not completed yet, EndInvoke will stop the calling thread until the asynchronous call is completed. The EndInvoke parameters include the out and ref parameters of the method to be asynchronously executed and the IAsyncResult returned by BeginInvoke.
(3)
AsyncCallback delegate is used to specify the method to be called after the operation is complete
The AsyncCallback delegate is passed as the second to the last parameter in the start operation.
The sample code is as follows:
[Serializable]
Public delegate void AsyncCallback (IAsyncResult ar );
AsyncCallback provides client applications with the method to complete asynchronous operations. When an asynchronous operation is started, the callback delegate is provided to the client. The event handler referenced by AsyncCallback contains the program logic for completing client asynchronous tasks.
AsyncCallback uses the IAsyncResult interface to obtain the status of asynchronous operations.
(4)
IAsyncResult Interface
It indicates the status of the asynchronous operation.
This interface defines four public attributes
The sample code is as follows:
Public interface IAsyncResult
In fact, there are four schemes for you to initiate and complete. NET asynchronous calls.
1. solution 1-use EndInvoke to wait for asynchronous call
The simplest method of asynchronous execution is to call the ininvoke method of the delegate to start the execution of the method, execute some work on the main thread, and then call the delegate EndInvoke method. EndInvoke may block the calling thread because it will not return until the asynchronous call is complete. This technology is very suitable for file or network operations, but because EndInvoke will block it, do not call it from the thread serving the user interface..
Let's start with your favorite console code:
1 using System;
2
3 namespace leleapplication1
4 {
5 class Class1
6 {
7 public delegate void AsyncEventHandler ();
8
9 void Event1 ()
10 {
11 Console. WriteLine ("Event1 Start ");
12 System. Threading. Thread. Sleep (2000 );
13 Console. WriteLine ("Event1 End ");
14}
15
16 void Event2 ()
17 {
18 Console. WriteLine ("Event2 Start ");
19 int I = 1;
20 while (I <1000)
21 {
22 I = I + 1;
23 Console. WriteLine ("Event2" + I. ToString ());
24}
25 Console. WriteLine ("Event2 End ");
26}
27
28 void CallbackMethod (IAsyncResult ar)
29 {
30 (AsyncEventHandler) ar. AsyncState). EndInvoke (ar );
31}
32
33
34 [STAThread]
35 static void Main (string [] args)
36 {
37 long start = 0;
38 long end = 0;
39 Class1 c = new Class1 ();
40 Console. WriteLine ("ready ");
41 start = DateTime. Now. Ticks;
42
43 AsyncEventHandler asy = new AsyncEventHandler (c. Event1 );
44 IAsyncResult ia = asy. BeginInvoke (null, null );
45 c. Event2 ();
46 asy. EndInvoke (ia );
47
48 end = DateTime. Now. Ticks;
49 Console. WriteLine ("time scale difference =" + Convert. ToString (end-start ));
50 Console. ReadLine ();
51}
52}
53}
54
This program is simple, and the asynchronous processing process is in the lines of code 43-46.
The result is as follows:
Now let's take a look at synchronization processing.
Modify the code lines 43-46:
C. Event1 ();
C. Event2 ();
The result is as follows:
The time scale of the former is much smaller than that of the latter
We can clearly see the speed superiority of Asynchronous Operation
2. solution 2-Round Robin asynchronous call completion
The IAsyncResult. IsCompleted attribute returned by BeginInvoke indicates whether the asynchronous operation has been completed.
This operation can be performed when asynchronous calling is performed from the service thread on the user interface. After the round robin is completed, the call thread is allowed to continue executing the asynchronous call on the ThreadPool thread.
Modify the code lines 43-46 again:
AsyncEventHandler asy = new AsyncEventHandler (c. Event1 );
IAsyncResult ia = asy. BeginInvoke (null, null );
C. Event2 ();
While (! Ia. IsCompleted)
{
}
Asy. EndInvoke (ia );
3. solution 3-use WaitHandle to wait for asynchronous call
The IAsyncResult. AsyncWaitHandle attribute obtains the WaitHandle used to wait for the completion of the asynchronous operation.
The WaitHandle. WaitOne method blocks the current thread until the current WaitHandle receives a signal.
When WaitHandle is used, other processing can be performed after the asynchronous call is complete, but before the EndInvoke result is called
Modify the code lines 43-46 again:
AsyncEventHandler asy = new AsyncEventHandler (c. Event1 );
IAsyncResult ia = asy. BeginInvoke (null, null );
C. Event2 ();
Ia. AsyncWaitHandle. WaitOne ();
4. solution 4-callback method executed when asynchronous call is completed
If the thread that starts the asynchronous call does not need to be the thread that processes the result, the callback method can be executed when the call is completed. The callback method is executed on the ThreadPool thread.
To use the callback method, you must pass the AsyncCallback delegate that references the callback method to BeginInvoke. You can also pass the object that contains the information to be used by the callback method. For example, you can pass the delegate that was used when the call was started so that the callback method can call EndInvoke.
Modify the code lines 43-46 again:
AsyncEventHandler asy = new AsyncEventHandler (c. Event1 );
Asy. BeginInvoke (new AsyncCallback (c. CallbackMethod), asy );
C. Event2 ();