Use IAsyncResult for. NET asynchronous programming

Source: Internet
Author: User

Microsoft provided a complete set of asynchronous programming design patterns as early as. net2.0, or VS2005. There are three common methods:

1. Use IAsyncResult to call the Asynchronous Method

2. Use Delegation for asynchronous programming

3. asynchronous multi-thread programming using events

Note: The IAsyncResult method is usually implemented by delegation. Therefore, we can consider the first two methods as one.

Aspx "> http://msdn.microsoft.com/zh-cn/library/2e08f6yc.aspx

Use IAsyncResult to call the Asynchronous Method

1. There are two matching methods: BeginXXX () and EndXXX (). They are asynchronous execution of the XXX () method.

The BegingXXX () parameter is added with AsyncCallback and AsyncState Based on the XXX () parameter.

The return value of EndXXX () is the same as that of XXX.

For example

 

Public delegate string AsyncMethodCaller (object param1, object param2 );

Public class AsyncDemo
{
 
Private AsyncMethodCaller _ asyncDelegate = new AsyncMethodCaller (Run );
 
Public IAsyncResult BeginRun (object param1, object param2, AsyncCallback asyncCallBack, Object asyncState)
 
{
 
AsyncDelegate. BeginInvoke (param1, param2, callBack, stateObject );
}
 
Public object EndRun (IAsyncResult asyncResult)
 
{
 
M_asyncDelegate.EndInvoke (asyncResult );
}
 
Public object Run (object param1, object param2 );
} 2. The IAsyncResult object stores information about asynchronous operations.

3. Several methods to prevent application execution

A. Stop asynchronous operations to prevent application execution

When EndXXX () is called, if the IAsyncResult object indicates that the asynchronous operation is not completed, the calling thread will be blocked, that is, the subsequent logic will be blocked.

 

IAsyncResult result = AsyncDemo. BeginRun (param1, param2, null, null );
 
AsyncDemo. EndRun (result );
 
// When Run () method is completed. Continue to go.
 
ContinueToGo ();

 

B. Use AsyncWaitHandle to prevent application execution.

Calling the corresponding method in IAsyncResult. AsyncWAitHandle after BeingXXX () can also block the calling thread. For example

 

IAsyncResult result = AsyncDemo. BeginRun (param1, param2, null, null );
 
// Wait until the operation completes.
 
Result. AsyncWaitHandle. WaitOne ();

C. Round-Robin the status of asynchronous operations.

After BeingXXX (), call the IAsyncResult. IsCompleted property to check whether this operation has been completed.

 

IAsyncResult result = AsyncDemo. BeginRun (param1, param2, null, null );
 
While (result. IsCompleted! = True)
 
{
 
// Waiting until IsCompleted is true.
}
 
// When Run () method is completed. Continue to go.
 
ContinueToGo (); d. Use AsyncCallback to end asynchronous operations.

The Endxxx () method is called in the proxy to allow the calling thread to continue running.

 

Public void MainFunc ()
 
{
 
AsyncCallback callBack = new AsyncCallback (ProcessRun );
 
IAsyncResult result = AsyncDemo. BeginRun (param1, param2, callBack, null );
 
ContinueToGo ();
 
}
 
Public void ProcessRun (IAsyncResult result)
{
 
AsyncDemo. EndRun (result );
 
}

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.