Exception after sending data async from a thread

If you have a situation like this:

void MyThreadFunc()
{
// do some work

socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(MyCallback), s);
}

void MyCallback(IAsyncResult ar)
{
Socket s = (Socket) ar.AsyncState;
s.EndSend(ar);
}

And you send a big buffer over a slow connection it can happen that calling EndSend() will throw a SocketException ‚The I/O operation has been aborted because of either a thread exit or an application request‘.

This happens because the Thread that started the sending is no more available when the sending ends. The only solution I know is to make sure the thread is still available or to use syncronous sending if possible.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.