开发者

Use of IAsyncResult.AsyncWaitHandle

In the asynchronous programming model, there looks to be 4 ways (As stated in Calling Synchronous Methods Asynchronously) for making asynchronous method calls.

Calling the EndInvoke() method makes the calling thread wait for the method completion and returns the result.

Going through the IAsyncResult.AsyncWaitHandle.WaitOne() also seem to do the same. AsyncWaitHandle gets a signal of completion (In other word the main thread waits for the Asynchronous method's completion). Then we can execute EndInvoke() to get the result.

  1. What is the difference between c开发者_Python百科alling the EndInvoke() directly and calling it after WaitOne()/WaitAll()?

  2. In the polling technique we provide time for other threads to utilize the system resources by calling Thread.Sleep(). Does AsyncWaitHandle.WaitOne() or EndInvoke() make the main thread go on sleep while waiting?


Q1. There is no difference in the way your code runs or your application, but there might be some runtime differences (again not sure, but a guess based my understanding of Async delegates).

  • IAsyncResult.AsyncWaitHandle is provided mainly as a synchronization mechanism while using WaitAll() or WaitAny() if you dont have this synchronization need you shouldn't read AsyncWaitHandle property. Reason : AsyncWaitHandle doesnt have to be implemented (created) by the delegate while running asynchronously, until it is read by the external code. I'm not sure of the way CLR handles the Async delegates and whether it creates a WaitHandler or not, but ideally if it can handle running your async delegates without creating another WaitHandle it will not, but your call to WaitOne() would create this handle and you have extra responsibility of disposing(close) it for efficient resource release. Therefore recommendation would be when there is no sycnchronization requirement which can be supported with WaitAll() or WaitAny() dont read this property.

Q2. This Question answers the difference between Sleep and Wait.


Simple things first. For your second question, yes, WaitOne and EndInvoke does indeed make the current thread sleep while waiting.

For your first questions, I can immediately identify 2 differences.

  1. Using WaitOne requires the wait handle to be released, while using EndInvoke directly doesn't require any cleanup.
  2. In return, using WaitOne allows for something to be done before EndInvoke, but after the task has been completed.

As for what that "something" might be, I don't really know. I suspect allocating resources to receive the output might be something that would need to be done before EndInvoke. If you really have no reason to do something at that moment, try not to bother yourself with WaitOne.


You can pass a timeout to WaitOne, so you could, for instance want to perform some other activities on a regular basis whilst waiting for the operation to complete:

do {
   //Something else
) while (!waitHandle.WaitOne(100))

Would do something every ~100 milliseconds (+ whatever the something else time is), until the operation completed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜