串口锁存应用程序C#
我正在制作一个应用程序,在WindowsForms应用程序屏幕中读取串行端口并更新数据 有时,当您试图关闭程序时,程序被锁定。 我正在使用委托 我应该做错什么串口锁存应用程序C#,c#,winforms,serial-port,C#,Winforms,Serial Port,我正在制作一个应用程序,在WindowsForms应用程序屏幕中读取串行端口并更新数据 有时,当您试图关闭程序时,程序被锁定。 我正在使用委托 我应该做错什么 void sp1_LineReceived(object sender, LineReceivedEventArgs Args) { Invoke(new Action(() => { // execute code })); } 初始化 输出代码 public partial class FormPrinci
void sp1_LineReceived(object sender, LineReceivedEventArgs Args)
{
Invoke(new Action(() =>
{
// execute code
}));
}
初始化
输出代码
public partial class FormPrincipal : Form
{
SerialPort spSimulador = new SerialPort();
public static PortaSerial spSistema = new PortaSerial();
一般来说,
BeginInvoke
比Invoke
更可取,因为它不会阻塞。你很可能会在这里陷入僵局
void sp1_LineReceived(object sender, LineReceivedEventArgs Args)
{
BeginInvoke(new Action(() =>
{
// execute code
}));
}
退出时是否正确关闭串行端口?在设备发送时使用Close()方法很可能导致死锁。在工作线程上使用BeginInvoke()或remove Close()或运行Close。
void sp1_LineReceived(object sender, LineReceivedEventArgs Args)
{
BeginInvoke(new Action(() =>
{
// execute code
}));
}