We have talked about C # delegation a lot. Here we talk about synchronous and asynchronous calls in C # delegation. I hope the examples in this article can help you in your daily work.
C # 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.
Example of synchronous call:
Using system;
Using system. Threading;
Public Delegate int addhandler (int A, int B );
Public class foo
{
Static void main ()
{
Console. writeline ("*********** syncinvoketest **************");
Addhandler handler = new addhandler (ADD );
Int result = handler. Invoke (1, 2 );
Console. writeline ("do other work .........");
Console. writeline (result); console. Readline ();
}
Static int add (int A, int B)
{Console. writeline ("computing" + A + "+" + B + "...");
Thread. Sleep (3000 );
Console. writeline ("computing complete .");
Return A + B;
}
}
Running result:
* ********** Syncinvoketest **************** computing 1 + 2... computing complete. do other work ......... 3 synchronous calls will block the thread. If it is to call a heavy job (such as a large number of Io operations), it may causeProgramThe pause takes a long time, causing a poor user experience. asynchronous calling is necessary at this time. Asynchronous calls do not block threads, but plug the calls into the thread pool. The main thread or UI thread of the program can continue to execute. The asynchronous call of the delegate is implemented through begininvoke and endinvoke.
Asynchronous call:
Using system;
Using system. Threading;
Public Delegate int addhandler (int A, int B );
Public class foo
{
Static void main ()
{
Console. writeline ("*********** asyncinvoketest **************");
Addhandler handler = new addhandler (ADD );
Iasyncresult result = handler. begininvoke (1, 2, null, null );
Console. writeline ("do other work .........");
Console. writeline (handler. endinvoke (result ));
Console. Readline ();
}
Static int add (int A, int B)
{
Console. writeline ("computing" + A + "+" + B + "...");
Thread. Sleep (3000 );
Console. writeline ("computing complete .");
Return A + B;
}
}
Running result:
* ********** Asyncinvoketest **************** do other work ......... computing 1 + 2... computing complete. 3
As you can see, the main thread does not wait, but runs down directly.
But the problem persists. When the main thread runs to endinvoke, if the call is not completed (this situation is likely to occur), the thread will still be blocked to wait for the call result.
The solution is to use a callback function. When the call ends, the callback function is automatically called.
Asynchronous callback:
Public class foo
{
Static void main (){
Console. writeline ("*********** asyncinvoketest **************");
Addhandler handler = new addhandler (ADD );
Iasyncresult result = handler. begininvoke (1, 2, new asynccallback (addcomplete), "asycstate: OK ");
Console. writeline ("do other work ......"); console. Readline ();
}
Static int add (int A, int B)
{
Console. writeline ("computing" + A + "+" + B + "...");
Thread. Sleep (3000 );
Console. writeline ("computing complete .");
Return A + B;
}
Static void addcomplete (iasyncresult result)
{
Addhandler handler = (addhandler) (asyncresult) result). asyncdelegate;
Console. writeline (handler. endinvoke (result ));
Console. writeline (result. asyncstate );
}
}