How to throw exception from asynch socket callback

  • Thread starter Thread starter Barry Anderberg
  • Start date Start date
B

Barry Anderberg

I am using asynch sockets and so I call BeginReceive and then in my
callback function I have certain events that would cause me to want to
throw an exception.

The problem of course is that the callback function is executing in a
separate thread from the main thread so when I throw the exception
it's apparently impossible to handle it in the calling thread (ie the
thread that called BeginReceive(..).

Is there a way to throw an exception to another thread like this?

Thanks,
Barry
 
Barry Anderberg said:
I am using asynch sockets and so I call BeginReceive and then in my
callback function I have certain events that would cause me to want to
throw an exception.

The problem of course is that the callback function is executing in a
separate thread from the main thread so when I throw the exception
it's apparently impossible to handle it in the calling thread (ie the
thread that called BeginReceive(..).

Is there a way to throw an exception to another thread like this?

If you want to process the the exception in the same thread that called
BeginReceive, then don't use async sockets. Or don't use a callback, and
call EndReceive.

If you are going to use async sockets, you will have to pass enough state to
the callback function for it to be able to handle the exception: you have to
move the appropriate exception handling context over callback thread.
Basically this involves creating a state object to hold what would otherwise
be local variables, and each of your callback functions reads and writes to
data in the state object, and always passes it on to the next callback.

David

This is really what makes async IO hard.

David
 
Back
Top