我有一个关于为什么我的C#接口在串行端口连接期间冻结的问题。如果我连接到有效的串行端口(我的设备发送了我期望的字节数),则该接口不会冻结。但是,如果用户尝试连接到计算机上的另一个端口或我的设备的错误模型,那么程序在发送'?'时不会返回任何数据。字符等等第一行:message [i] =(byte)port.BaseStream.ReadByte();导致超时并落入我的catch并尝试连接3次,然后警告用户连接失败。在三次尝试完成后,用户界面工作正常,但在进行用户界面时无法响应。有任何想法吗?在此先感谢,下面是我的代码。串行端口尝试连接时C#UI冻结
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)
{
以上是我通过DispatcherTimer对象连续运行的代码,每隔10 ms运行一次。