2014-01-28 27 views
0

我正在製作一個應用程序,它正在讀取串口並在WindowsForms應用程序屏幕中更新數據。串口鎖存器應用程序C#

有時,當你試圖關閉該程序被鎖定。 我在使用委託。

我該做什麼錯?

void sp1_LineReceived(object sender, LineReceivedEventArgs Args) 
{ 
    Invoke(new Action(() => 
    { 
    // execute code 
    })); 
} 

初始化

public FormPrincipal() 
{ 
    InitializeComponent(); 
    spSistema.LineReceived += new LineReceivedEventHandler(sp1_LineReceived); 
    // next codes 
} 

Outher碼

public partial class FormPrincipal : Form 
{  
    SerialPort spSimulador = new SerialPort();   
    public static PortaSerial spSistema = new PortaSerial(); 
+0

你正確關閉退出串口? –

+1

當設備發送時使用Close()方法時,很可能導致死鎖。使用BeginInvoke()或刪除Close()或在工作線程上運行Close。 –

回答

2

一般來說,BeginInvoke優選Invoke,因爲它不會阻塞。 See here。你可能在這裏陷入僵局。

void sp1_LineReceived(object sender, LineReceivedEventArgs Args) 
{ 
    BeginInvoke(new Action(() => 
    { 
    // execute code 
    })); 
}