我正在使用線程來調用包含while循環來讀取權重的函數。在while循環中,我調用委託函數來更新文本框中的值。線程在C#中使用帶代理的線程中止異常
在點擊一個名爲Stop
按鈕,我試圖中止線程,但我得到一個線程中止異常:
private System.Threading.Thread comm1;
comm1 = new System.Threading.Thread(new System.Threading.ThreadStart(reading));
comm1.Start();
public void reading()
{
while(continus)
{
textBox1.Invoke(
new PrintValueTextDelegate(PrintValueText),
new object[] { text Box, value.ToString() });
for (int i = 0; i < risposta.Length; i++)
{
risposta[i] = 0;
}
if (_protocollo.Manda_TLC1(2, 0x70, risposta) == true)
{
if ((risposta[0] & 0x80) != 0x80)
{
cella = risposta[1] * 256 + risposta[2];
string rt = cella.ToString();
}
}
}
}
private void btnstop_Click(object sender, EventArgs e)
{
try
{
continus = false;
System.Threading.Thread.Sleep(1000);
comm1.abort(); // wait for close foreground thread
}
catch (Exception rt)
{
MessageBox.Show(rt.ToString());
}
}
上面的代碼我得到線程終止異常可以在任何一個請幫助我出了這個問題。
幫我解決...... **爲什麼**你想要調用thread.abort?這是一種非常致命的方法......你有什麼具體的*試圖在這裏做? (「什麼」而不是「如何」) –
Thread.Abort()通過設計引發異常,除了用try-catch塊隱藏它外沒有辦法繞過它 – Alex
我試圖從連續讀取體重線程並在文本框中顯示重量。當重量校準結束時,我正在嘗試關閉它。 –