I have a question concerning why my C# interface is freezing during a serial port connection. If I connect to a serial port that is valid (my device which sends the number of bytes I am expecting) the interface doesn't freeze. However if the user attempts to connect to another port on the computer or the wrong model of my device the program gets no data back when it is send a '?' character and so the first line: message[i] = (byte)port.BaseStream.ReadByte(); causes a timeout and falls into my catch and attempt to connect 3 times then alerts the user of the failed connection. The UI works fine aftet the three attempts are done, but while they are going on the UI is non responsive. Any ideas? Thanks in advance, below is my code.
public void windowLoop(object sender, EventArgs e)
{
if (Global.connecting)
{
try
{
if (i == 0) { port.BaseStream.Flush(); port.BaseStream.WriteByte(63); }
message[i] = (byte)port.BaseStream.ReadByte();
if (i == 6)
{
connectAttempts = 1;
i = 0;
String result = Encoding.ASCII.GetString(message);
if (result == "ASUTTON")
{
//connection succeeded
Global.connecting = false;
Global.receiving = true;
setDisplay(2);
}
else
{
//connection failed
Global.connecting = false;
disconnect();
MessageBox.Show(radio, "You are not connection to an Agent (Radio Version)", "Connection Failed", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
else
{
i++;
}
}
catch
{
if (connectAttempts >= connectAttemptsLimit)
{
connectAttempts = 1;
Global.connecting = false;
disconnect();
MessageBox.Show(radio, "Your device failed to connect to the program.", "Connection Failed", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
connectAttempts++;
}
}
}
else if (Global.sending)
{
Above is my code that is run continuously via a DispatcherTimer object set to run every 10 ms.