Share via


IAsyncResult Implementation Compliant

There is an internal discussion around IAsyncResult implmentation, and the developer responsible for IAsyncResult posted all the invariants that a compliant implementation has to support.

Posted below for your information:


From:  
Sent: Monday, March 27, 2006 4:28 PM
To:

Subject: RE: IAsyncResult implementation

 

A while back we did a project to lay out all the invariants that a compliant implementation has to support. These are both necessary and sufficient, and are from the point of view of the IAsyncResult consumer. (That is, as long as the properties behave this way, it doesn’t matter what the implementation is.)

1. IsCompleted can change from false to true, but never from true to false.

2. CompletedSynchronously never changes.

3. If CompletedSynchronously is true, IsCompleted is true.

4. If AsyncWaitHandle is signaled, IsCompleted is true.

5. If queried from within the callback, AsyncWaitHandle is signaled.

6. EndXxx doesn’t block if IsCompleted is true.

7. The callback is called exactly once, unless BeginXxx throws, in which case it isn’t called.

8. AsyncWaitHandle is valid (not disposed) until EndXxx is called.

9. The same instance of IAsyncResult is returned from BeginXxx and passed into the callback.

10. BeginXxx doesn’t block.

11. AsyncState returns the state object passed to BeginXxx.

Comments

  • Anonymous
    March 29, 2006
    This is very useful. Can you describe the scenarios under which CompletedSynchronously is true and how this should be handled in a callback? For example, how to deal with posting a new BeginReceive within a callback that was completed synchronously.

    Thanks
  • Anonymous
    March 29, 2006
    This place is not clear:

    8. AsyncWaitHandle is valid (not disposed) until EndXxx is called

    Does it mean implementer of BeginXxx/EndXxx and IAsyncResult required to Dispose AsyncWaitHandle?

    Documentation state that disposing AsyncWaitHandle is caller's responsibility, not IAsyncResult implementer. I never liked this place, because it may IAsyncResult leaky.

    So, with that new guideline any IAsyncResult implementer MUST dispose AsyncWaitHandle on EndXxx if it was allocated. Yes or No?

    Thank you very much.
    ---
    Oleg Mihailik, Developer Security MVP
    Kyiv, Ukraine
  • Anonymous
    March 29, 2006
    Sorry folks. Maybe it is not clear but I don't work in that area and I can't answer those questions.

    This is restrictly FYI, no question answered.
  • Anonymous
    March 29, 2006
    Thank you anyway, Junfeng.
  • Anonymous
    April 06, 2006
    I think you're missing at least one rule:

    "There must be at least one circumstance under which 'IsCompleted' will return true"

    You can't make a tighter rule ('IsCompleted will eventually become true') becase there are always odd cases where it's not true.

    Not having the rule means that software can be contractually correct and yet be useless; that's always a bad thing.