Morgan Cheng wrote:
[...]
I am wondering. When the asynchronous is complete, is
IAsyncResult.AsyncWaitHandle be signaled first, or AsyncCallback be
invoked first?
Surely they are effectively simultaneous. That is, obviously one
happens before the other. But they likely occur in practically
immediate sequence, and because of the way thread scheduling works on
Windows, it's entirely possible that if you have both a callback and a
thread waiting on the WaitHandle, that either the code in the callback
or the code waiting on the WaitHandle could get scheduled next.
In other words, even if one or the other is technically always done
first, the observed result could in fact be the opposite. I would
suggest that this is one of those things that you should not assume is
well-defined and should not rely on a specific behavior in code,
especially if you are asking as a general question related to anything
that uses IAsyncResult (even if the behavior is well-defined for a
particular class, I'm not sure you could depend on it being well-defined
for _all_ classes that implement the asynchronous paradigm).
I'm curious if there's a specific scenario in which this question came
up? It seems like an interesting enough academic question, but since
you would normally use _either_ the WaitHandle _or_ a callback, not both
together, it's not clear why this would come up in actual code.
Pete