Serial port unable to dispose nicely

S

Shmuel Naaman

Help please, I created a win form app with a serial port component, if
the user disconnect the cable without closing the port first no
exception is throwing but when the user close the form I get an
exception that I cant catch – unauthorized exception .I tried to use a
timer that checks the .IsOpend and closes the port but no lock – I
steel get the same error.
I tried to Dispose/ set to null/ create a back thred – nothing yet.
Is it something with the properties of the serial port?
Can someone send a working sample of a form that I can disconnect the
cable and still close the form nicely?
thanks
 
K

Ken Foskey

Help please, I created a win form app with a serial port component, if
the user disconnect the cable without closing the port first no
exception is throwing but when the user close the form I get an
exception that I cant catch – unauthorized exception .I tried to use a
timer that checks the .IsOpend and closes the port but no lock – I steel
get the same error.
I tried to Dispose/ set to null/ create a back thred – nothing yet. Is
it something with the properties of the serial port? Can someone send a
working sample of a form that I can disconnect the cable and still close
the form nicely? thanks


Here is a partial of the class that I am using, it is running on a
background thread. The form close sets "running" to false. The timeout
stops the receive every 5 seconds to check. So the socket is only locked
for 5 seconds when the form closes at the most.

No doubt others will pick me up on some problems with the code :)


class receive
{
bool running;

/// <summary>
/// The method that will be called when the thread is started.
/// </summary>
public void InstanceMethod()
{
// Create a TCP/IP socket.
listener = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
byte[] buff = new byte[2000];

// Bind the socket to the local endpoint and listen for
incoming connection
try
{
listener.EnableBroadcast = true;
listener.Bind(localEndPoint);
listener.ReceiveTimeout = 10000;

RaiseError("Transfer", String.Format("Waiting at {0}",
localEndPoint),
ErrorLevel.message, null);

while (running)
{
EndPoint senderRemote = (EndPoint)remoteEndPoint;

// Start an asynchronous socket to listen for
connections.
int count = 0;
try
{
count = listener.ReceiveFrom(buff, 0,
buff.Length, SocketFlags.None, ref senderRemote);
}
catch (SocketException ex)
{
if (ex.ErrorCode != 10060) // ignore time out
throw ex;
}
String rcvd = Encoding.ASCII.GetString(buff, 0,
count);

if (receipt != null && count > 0)
{
receipt(this, new TransferMessage(rcvd));
}
}

}
catch (SocketException e)
{
String message = String.Format("Communication error:
{0}", e.ToString());
RaiseError("Transfer:Receive", message, ErrorLevel.major,
e);
}
finally
{
listener.Close();
listener = null;
RaiseError("Transfer", String.Format("Shudown UDP {0}",
localEndPoint),
ErrorLevel.message, null);
}
}

}
 

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