使用带有委托的线程的 C# 中的线程中止异常

发布于 2024-12-13 19:38:35 字数 1186 浏览 0 评论 0原文

我正在使用一个线程来调用一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

完美的未来在梦里 2024-12-20 19:38:35

更改

comm1.abort();// wait for close foreground thread 

comm1.join();// wait for close foreground thread 

abort 会立即停止线程,而 join 则会等待线程完成。

change

comm1.abort();// wait for close foreground thread 

to

comm1.join();// wait for close foreground thread 

abort halts the thread immediately, whereas join waits for it to finish.

樱娆 2024-12-20 19:38:35

您收到线程中止异常,因为您告诉线程中止...

http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx(这目前对我不起作用,但我从谷歌的缓存)说:

调用Abort方法时抛出的异常。

请注意,这不是告诉您对 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:

The exception that is thrown when a call is made to the Abort method.

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.

情何以堪。 2024-12-20 19:38:35

调用 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"

瞄了个咪的 2024-12-20 19:38:35

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文