中止.NET线程

问题描述:

我创建一个线程A和线程创建一个新的线程B.中止.NET线程

因此,如何是线程层次?线程B是线程A的子进程?或者线程创建为同伴?

我要中止父线程A这反过来又杀死/中止其子线程。

怎么可能在C#中?

+0

你应该解释为什么你想中止这些线程。 – apoorv020 2010-06-24 08:31:29

+0

C#没有线程。你的意思是“.NET线程”。 – 2010-07-15 16:36:31

线程应该非常永远被中止。它根本不安全。把这看作是放下已经生病的过程的一种方式。否则,避免像瘟疫一样。

这样做的更正确的方法是让代码可以定期检查,并且本身决定退出。

+0

f.e.只是空回报声明? – Xorty 2010-06-24 07:10:18

+0

@Xorty - 这取决于场景,它在做什么,但也许。也许是一个例外。也许'打破;' – 2010-06-24 07:26:29

Thread.Abort会做你想做的,但不建议中止线程,更好的选择是想办法通过Thread synchronization机制

线程是同龄人的正确创建完成的线程,获取句柄到线程A然后调用ThreadA.Abort() 强行结束它。最好在线程中检查一个布尔值,并且如果它的计算结果为false,则退出该线程。

public class MyClass 
{ 
    public static Thread ThreadA; 
    public static Thread ThreadB; 

    private void RunThings() 
    { 
     ThreadA = new Thread(new ThreadStart(ThreadAWork)); 
     ThreadB = new Thread(new ThreadStart(ThreadBWork)); 

     ThreadA.Start(); 
     ThreadB.Start(); 
    } 

    static void ThreadAWork() 
    { 
     // do some stuff 

     // thread A will close now, all work is done. 
    } 


    static void ThreadBWork() 
    { 
     // do some stuff 

     ThreadA.Abort(); // close thread A 

     // thread B will close now, all work is done. 
    } 
} 

停止线程的礼貌的方式的一个例子:

using System; 
using System.Threading; 

namespace Treading 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Main program starts"); 
      Thread firstThread = new Thread(A); 
      ThreadStateMessage messageToA = new ThreadStateMessage(){YouShouldStopNow = false}; 
      firstThread.Start(messageToA); 
      Thread.Sleep(50); //Let other threads do their thing for 0.05 seconds 
      Console.WriteLine("Sending stop signal from main program!"); 
      messageToA.YouShouldStopNow = true; 
      firstThread.Join(); 
      Console.WriteLine("Main program ends - press any key to exit"); 
      Console.Read();// 
     } 

     private class ThreadStateMessage 
     { 
      public bool YouShouldStopNow = false; //this assignment is not really needed, since default value is false 
     } 

     public static void A(object param) 
     { 
      ThreadStateMessage myMessage = (ThreadStateMessage)param; 
      Console.WriteLine("Hello from A"); 
      ThreadStateMessage messageToB = new ThreadStateMessage(); 
      Thread secondThread = new Thread(B); 
      secondThread.Start(messageToB); 

      while (!myMessage.YouShouldStopNow) 
      { 
       Thread.Sleep(10); 
       Console.WriteLine("A is still running"); 
      } 

      Console.WriteLine("Sending stop signal from A!"); 
      messageToB.YouShouldStopNow = true; 
      secondThread.Join(); 

      Console.WriteLine("Goodbye from A"); 
     } 

     public static void B(object param) 
     { 
      ThreadStateMessage myMessage = (ThreadStateMessage)param; 
      Console.WriteLine("Hello from B"); 
      while(!myMessage.YouShouldStopNow) 
      { 
       Thread.Sleep(10); 
       Console.WriteLine("B is still running"); 
      } 
      Console.WriteLine("Goodbye from B"); 
     } 
    } 
} 

使用Thread.Abort的();如果您的线程处于任何类型的等待状态,则会导致抛出异常。由于线程可以等待的方式有很多种,所以处理起来很麻烦。正如其他人所说,你通常应该避免这样做。

+1

你应该声明'YouShouldStopNow'成员为'volatile' – Liran 2010-08-03 14:06:09

+0

谢谢,我不知道那一个。我想这对于在第一次轮询时需要中断的非常快速的循环来说是非常重要的。 – LaustN 2010-09-13 12:58:05

这里是另一种方式来礼貌地信号线程死:

注意,这有利于时装有限状态自动机,其中从定期检查住权限,那么如果允许执行任务。任务不会中断,并且是“原子”的。这对于简单的循环或命令队列很有效。另外这可以确保线程不被给予从线程休息一段时间,这一个设置为0,如果你不从你想要的任何其他旋转100%的CPU。