最近在看MSDN上异步相关的内容,摘录了一些以便查阅。
The .NET Framework provides two design patterns for asynchronous operations:
- Asynchronous operations that use IAsyncResult objects.
- Asynchronous operations that use events.
Pasted from <http://msdn.microsoft.com/en-us/library/ms228969.aspx>
Asynchronous operations that use the IAsyncResult design pattern
- Implement BeginOperationName and EndOperationName methods.
- BeginOperationName returns an object that implements IAsyncResult interface. BeginOperationName takes any parameters that declared in the synchronous version method. There're two optional parameters including AsyncCallback delegate and a user-defined object that could be used to pass application-specific state information.
- EndOperationName takes any parameters that declared in the synchronous version method. Besides those, it also takes IAsyncResult that returned by related BeginOperationName.
- If asynchronous operation doesn't complete when call EndOperationName, the calling thread will be blocked until the operation completes.
Calling Asynchronous Methods Using IAsyncResult
Blocking Application Execution by Ending an Asynchronous Operation
- Call method EndOperationName to block application until the asynchronous operation completes
Blocking Application Execution Using an AsyncWaitHandle
- Call WaitOne method on AsyncWaitHandle property of IAsyncResult object which is returned by BeginOperationName method.
- To block while waiting for a set of asynchronous operations to complete, store the associated AsyncWaitHandle objects in an array and call one of the WaitAll methods. To block while waiting for any one of a set of asynchronous operations to complete, store the associated AsyncWaitHandle objects in an array and call one of the WaitAny methods
|
Note: |
|
When you use the BeginInvoke method of a delegate to call a method asynchronously and obtain a wait handle from the resulting IAsyncResult, we recommend that you close the wait handle as soon as you are finished using it, by calling the WaitHandle.Close method. If you simply release all references to the wait handle, system resources are freed when garbage collection reclaims the wait handle, but garbage collection works more efficiently when disposable objects are explicitly closed or disposed. For more information, see the AsyncResult.AsyncWaitHandle property. |
Pasted from <http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncwaithandle.aspx>
Polling for the Status of an Asynchronous Operation
- Test the if IsCompleted property is true on IAsyncResult object which is returned by BeginOperationName method.
Using an AsyncCallback Delegate to End an Asynchronous Operation
- Create an AsyncCallback delegate and pass it to BeginOperationName method.

浙公网安备 33010602011771号