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.
Example of synchronous calls:
using System;
using ;
public delegate int AddHandler(int a, int b);
public class Foo {
static void Main() {
("**********SyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
int result = (1,2);
("Do other work... ... ...");
(result);
();
}
static int Add(int a, int b) {
("Computing "+a+" + "+b+" ...");
(3000);
("Computing Complete.");
return a+b;
}
}Run result:
**********SyncInvokeTest**************
Computing 1 + 2 ...
Computing Complete.
Do other work... ... ...
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 be paused for a long time and cause bad luck.
Asynchronous calls are necessary at this time.
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.
Asynchronous call:
using System;
using ;
public delegate int AddHandler(int a, int b);
public class Foo {
static void Main() {
("**********AsyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
IAsyncResult result = (1,2,null,null);
("Do other work... ... ...");
((result));
();
}
static int Add(int a, int b) {
("Computing "+a+" + "+b+" ...");
(3000);
("Computing Complete.");
return a+b;
}
}Run result: *************AsyncInvokeTest***************
Do other work... ... ...
Computing 1 + 2 ...
Computing Complete.
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.
The solution is to use a callback function, which will automatically call the callback function when the call is finished.
Callback asynchronous:
public class Foo {
static void Main() {
("**********AsyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
IAsyncResult result = (1,2,new AsyncCallback(AddComplete),"AsycState:OK");
("Do other work... ... ...");
();
}
static int Add(int a, int b) {
("Computing "+a+" + "+b+" ...");
(3000);
("Computing Complete.");
return a+b;
}
static void AddComplete(IAsyncResult result) {
AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
((result));
();
}
}
Example of synchronous calls:
Copy the codeThe code is as follows:
using System;
using ;
public delegate int AddHandler(int a, int b);
public class Foo {
static void Main() {
("**********SyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
int result = (1,2);
("Do other work... ... ...");
(result);
();
}
static int Add(int a, int b) {
("Computing "+a+" + "+b+" ...");
(3000);
("Computing Complete.");
return a+b;
}
}Run result:
**********SyncInvokeTest**************
Computing 1 + 2 ...
Computing Complete.
Do other work... ... ...
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 be paused for a long time and cause bad luck.
Asynchronous calls are necessary at this time.
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.
Asynchronous call:
Copy the codeThe code is as follows:
using System;
using ;
public delegate int AddHandler(int a, int b);
public class Foo {
static void Main() {
("**********AsyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
IAsyncResult result = (1,2,null,null);
("Do other work... ... ...");
((result));
();
}
static int Add(int a, int b) {
("Computing "+a+" + "+b+" ...");
(3000);
("Computing Complete.");
return a+b;
}
}Run result: *************AsyncInvokeTest***************
Do other work... ... ...
Computing 1 + 2 ...
Computing Complete.
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.
The solution is to use a callback function, which will automatically call the callback function when the call is finished.
Callback asynchronous:
Copy the codeThe code is as follows:
public class Foo {
static void Main() {
("**********AsyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
IAsyncResult result = (1,2,new AsyncCallback(AddComplete),"AsycState:OK");
("Do other work... ... ...");
();
}
static int Add(int a, int b) {
("Computing "+a+" + "+b+" ...");
(3000);
("Computing Complete.");
return a+b;
}
static void AddComplete(IAsyncResult result) {
AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
((result));
();
}
}