【发布时间】:2020-08-18 23:28:06
【问题描述】:
我正在使用线程调用包含 while 循环的函数来读取权重。在 while 循环中,我正在调用一个委托函数来更新文本框中的值。
单击名为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 块隐藏它之外别无他法
-
我正在尝试从线程中连续读取重量并在文本框中显示重量。当重量校准结束时,我正在尝试关闭它。
标签: c# delegates threadabortexception