ASP. NET multi-threaded programming-asynchronous programming (9)

Source: Internet
Author: User

  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 ();

}

  

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.