4

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 calling 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?

SaravananArumugam
  • 3,680
  • 6
  • 33
  • 45

3 Answers3

6

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.

Community
  • 1
  • 1
Sanjeevakumar Hiremath
  • 10,985
  • 3
  • 41
  • 46
4

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.

Fun Mun Pieng
  • 6,751
  • 3
  • 28
  • 30
1

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.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • wouldn't it be better in this case to test **[`IAsyncResult.IsCompleted`](http://msdn.microsoft.com/en-us/library/system.iasyncresult.iscompleted.aspx)**, instead of putting the thread to sleep? The MSDN page says: _"Clients that poll for operation status (as opposed to waiting on a synchronization object) use this property to determine the status of the operation."_ – stakx - no longer contributing Mar 21 '11 at 09:02
  • @stackx - but I am waiting on a synchronization object - I'm using it because I want to sleep for some period of time, but if that task completes during that sleep period, my code is immediately reactivated and exits its loop (of doing other things) – Damien_The_Unbeliever Mar 21 '11 at 09:09
  • I understand that. But my question is, why does your loop need to be interrupted by a sleep/wait *at all*? Why not skip the sleep/wait and get the stuff in the loop done faster? What is the specific advantage that you were thinking of here? – stakx - no longer contributing Mar 21 '11 at 09:55