Reading directory
I. synchronous processing
Ii. asynchronous Processing
Iii. asynchronous Delegation
Iv. Call Methods synchronously through delegation
V. Call Methods asynchronously through delegation
I. synchronous processing
A synchronization operation will block the entire process until the operation is completed before the next code segment can be executed.
Ii. asynchronous Processing
The call thread of the startup operation is not blocked. The caller must take turns to detect the call or wait for the Completion signal to find the completion of the call.
Iii. asynchronous Delegation
.The ability of asynchronous delegation to call synchronous methods in asynchronous mode is also described in the synchronous call of WebService and asynchronous call of WebService. The same Web service, because of the different call methods, synchronous call and asynchronous call are implemented. synchronous is called directly.
.When a delegate is synchronously called, The Calling method is directly the target method of the current thread. Here it refers to the GetNameFirst () method. If the compiler supports asynchronous delegation, it will generate BeginInvoke () method and EndInvoke () method
.As long as any function is called directly in our compiler, there is also an asynchronous call, that is, BeginInvoke (), EndInvoke (), BeginInvoke () the returned result is to determine whether the execution is complete, and EndInvoke () returns the call result.
Iv. Call Methods synchronously through delegation
Class User
{
// Dynamic method to be called
Public string GetNameFirst ()
{
Thread. Sleep (30000 );
Return "getting sleepy from an early age ";
}
// Static method to be called
Public static string GetNameSecond ()
{
Return "getting sleepy from an early age ";
}
}
// Delegate Declaration (function signature)
Delegate string MyMethodDelegate ();
Static void Main (string [] args)
{
User user = new User ();
// Method 1: Call the method synchronously, declare a delegate variable mydelegate, and bind it to the dynamic method GetNameFirst
MyMethodDelegate my_delegate = new MyMethodDelegate (user. GetNameFirst );
String strResult = my_delegate ();
Console. WriteLine (strResult );
Console. ReadKey ();
}
Because we set the current thread to sleep for 30 seconds, we have to wait 30 seconds before we can output "getting sleepy from an early age"
V. Call Methods asynchronously through delegation
Using System. Runtime. Remoting. Messaging;
Delegate string MyMethodDelegate ();
Static void Main (string [] args)
{
// Method 2: Call the method asynchronously, declare a delegate variable mydelegate, and bind it to the dynamic method GetNameSecond
MyMethodDelegate my_delegate = new MyMethodDelegate (User. GetNameSecond );
AsyncResult async_result; // The result of this closed asynchronous delegate asynchronous call. The result is obtained through AsyncResult.
// Start calling
Async_result = (AsyncResult) my_delegate.BeginInvoke (null, null );
// Determine whether the thread execution is complete
While (! Async_result.IsCompleted)
{
Console. WriteLine ("Asynchronous execution method GetNameSecond ()......");
}
Console. WriteLine ("method GetNameSecond () Execution completed ");
// Wait for the completion of the delegate call Method
String strResult = my_delegate.EndInvoke (async_result );
Console. WriteLine (strResult );
Console. ReadKey ();
}