private void SendC(SendStrC sendSetC) { txtdataC.Clear(); ReceiveDataCClear(); receiveDataCntC = 0; if (SendingC == true) { return; //如果当前正在发送,则取消本次发送,本句注释后,可能阻塞在ComSend的lock处 } _ComSendC = new Thread(new ParameterizedThreadStart(ComSendC)); //new发送线程 _ComSendC.Start(sendSetC); //发送线程启动 }
private void ComSendC(object objC) { lock (this) //由于send()中的if(Sending == true) return,所以这里不会产生阻塞,如果没有那句,多次启动该线程,会在此处排队 { if (port != null) { try { SendingC = true; SendStrC tempC = (SendStrC)objC; byte[] comSendDataC = tempC.SendData; port.Write(comSendDataC, 0, comSendDataC.Length); SendingC = false; } catch (Exception err) { MessageBox.Show("命令发送失败:" + err.Message, "提示"); } } } }