使用带有委托的线程的 C# 中的线程中止异常
我正在使用一个线程来调用一个包含 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());
}
}
对于上述代码,我收到线程中止异常,任何人都可以帮我解决这个问题问题。
I am using a thread to call a function containing a while loop to read a weights. In the while loop, I am calling a delegate function to update values in a text box.
On clicking a button named Stop
, I am trying to abort the thread, but I am getting a Thread Abort exception:
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());
}
}
For the above code I am getting thread abort exception can any one please help me out with this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更改
为
abort
会立即停止线程,而join
则会等待线程完成。change
to
abort
halts the thread immediately, whereasjoin
waits for it to finish.您收到线程中止异常,因为您告诉线程中止...
http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx(这目前对我不起作用,但我从谷歌的缓存)说:
请注意,这不是告诉您对 Thread.Abort() 的调用失败的异常,而是您正在中止的线程的异常,表示“啊!我刚刚被中止了!”。
如果你想更优雅地停止,那么让你的 stop 调用将 while 循环中的
continus
变量更改为 false。然后 while 循环将停止运行,您的线程应该完成。You are getting a thread abort exception because you are telling the thread to abort...
http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx (whcih isn't working for me at the moment but I grabbed text from google's cache) says:
Note that this is not an exception telling you that your call to Thread.Abort() has failed, it is an exception from the thread you are aborting saying "Argh! I've just been aborted!".
If you want to stop more gracefully then make your stop call change your
continus
variable in your while loop to false. Then the while loop will stop running and your thread should finish.调用 Thread.Abort() 会抛出线程中止异常,这就是它的工作原理。
请参阅文档“在调用它的线程”
Calling
Thread.Abort()
throws a thread abort exception, that is how it works.See the docs "Raises a ThreadAbortException in the thread on which it is invoked"
使用 CancellationTokenSource 向线程发出取消操作。请注意,您的申请必须检查是否已请求取消。
使用 thread.Join 等待线程退出。
Use CancellationTokenSource to issue a cancel operation to a thread. Note that your application must check if cancellation has been requested.
Use thread.Join to wait for a thread to exit.