2010-06-24 84 views
3

我創建一個線程A和線程創建一個新的線程B.中止.NET線程

因此,如何是線程層次?線程B是線程A的子進程?或者線程創建爲同伴?

我要中止父線程A這反過來又殺死/中止其子線程。

怎麼可能在C#中?

+0

你應該解釋爲什麼你想中止這些線程。 – apoorv020 2010-06-24 08:31:29

+0

C#沒有線程。你的意思是「.NET線程」。 – 2010-07-15 16:36:31

回答

11

線程應該非常永遠被中止。它根本不安全。把這看作是放下已經生病的過程的一種方式。否則,避免像瘟疫一樣。

這樣做的更正確的方法是讓代碼可以定期檢查,並且本身決定退出。

+0

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

+0

@Xorty - 這取決於場景,它在做什麼,但也許。也許是一個例外。也許'打破;' – 2010-06-24 07:26:29

1

線程是同齡人的正確創建完成的線程,獲取句柄到線程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. 
    } 
} 
6

停止線程的禮貌的方式的一個例子:

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

3

這裏是另一種方式來禮貌地信號線程死:

注意,這有利於時裝有限狀態自動機,其中從定期檢查住權限,那麼如果允許執行任務。任務不會中斷,並且是「原子」的。這對於簡單的循環或命令隊列很有效。另外這可以確保線程不被給予從線程休息一段時間,這一個設置爲0,如果你不從你想要的任何其他旋轉100%的CPU。