This article will mainly explain the differences and advantages and disadvantages of using the "synchronous call", "Asynchronous call", and "Asynchronous callback" to execute the same "addition class" with a delegate.
First, use the code to define a delegate and the methods to be called in the following three examples:
/* Added namespace
Using System. Threading;
Using System. Runtime. Remoting. Messaging;
*/
Public delegate int AddHandler (int a, int B );
Public class addition class
{
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;
}
}
Synchronous call
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.
Synchronous call of public class
{
Static void Main ()
{
Console. WriteLine ("===== synchronously call SyncInvokeTest ==== ");
AddHandler handler = new AddHandler (addition class. Add );
Int result = handler. Invoke (1, 2 );
Console. WriteLine ("continue to do other things... ");
Console. WriteLine (result );
& Nbs