我有一個關於爲什麼我的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運行一次。