我正在使用线程来调用包含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
我试图从连续读取体重线程并在文本框中显示重量。当重量校准结束时,我正在尝试关闭它。 –