Peter Duniho
10/2/2008 10:49:00 AM
On Thu, 02 Oct 2008 03:22:11 -0700, icomply <kerplunkwhoops@yahoo.co.uk>
wrote:
> [...]
> Can anyone confirm this is the correct behaviour of BeginRead? Should
> I assume that BeginRead can actually execute the callback function
> within the BeginRead statement, and on same thread? Could something
> else I do cause this to happen? Is the thread pool being corrupted?
This sounds like reasonable behavior to me.
At the very least, you should not expect a call to BeginRead() to
necessarily create a new thread. In fact, in most cases it won't.
Instead, it will initiate the i/o operation, and an already-existing
thread will dequeue an i/o completion event when the operation completes.
In the scenario you describe, apparently the i/o operation can be
completed immediately, and .NET doesn't bother to initiate an asynchronous
operation but instead simply completes it immediately. That seems fine to
me.
What this has to do with your apparent deadlock, I can't say. Your code
shouldn't rely on i/o operations being completed on any specific thread,
but in any case having an operation complete on the same thread on which
it was started seems to me more likely to reduce the chance of deadlock
than increase it. Though admittedly, it depends on the nature of the
lock, so if what you're waiting on isn't a synchronization object, but
rather some operation you expected your thread to finish after returning
from BeginRead(), then yes...that certainly could create a deadlock
situation.
Pete