How to determine if a socket is disposed

J

John J. Hughes II

In my code I have the below callback. The socket I am connected to is
dropped by the other end it would be nice to not set off the exception. As
you can see from the code I check if r is null and if the async state is
null but I am still getting the below error.

Is there a better way of handling this?

Regards,
John

Code:
private void Send_Callback(System.IAsyncResult r)
{
try
{
if(r!=null && r.AsyncState != null)
((Socket)r.AsyncState).EndSend(r);
}
catch(System.Threading.ThreadAbortException)
{
System.Diagnostics.Debug.WriteLine("Monitor thread worker (Send
callback):Aborting");
}
catch(Exception ex)
{
this.onException(ex);
Utils.frmError.WriteError("Monitor thread worker (Send callback):", ex);
}
}

Error:

Exception:Cannot access a disposed object named "System.Net.Sockets.Socket".
Object name: "System.Net.Sockets.Socket".
Stack Trace: at System.Net.Sockets.Socket.EndSend(IAsyncResult
asyncResult)
at RAMSComThrd.MonitorThrdWorker.Send_Callback(IAsyncResult r) in
MonitorThrd.cs:line 264
Source:System
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top