2013-06-05 168 views
1

說我有這樣的代碼:線程是否可以調用線程?

public int A = 0; 

//This is the method that will 
//be run as a thread 
public void Thread1() 
{ 
    public bool continue = true; 
    while (continue == true) 
    { 
     if (A==2) 
     { 
      Thread t2 = new Thread(new ThreadStart(Thread2)); 
     } 

     //Some other code here 
    } 

} 

//This is the method that Thread1 
//will try to run if A = 2 
public void Thread2() 
{ 
    //Coding in this thread 
} 

再說說INT A收到來自其他方法或類似的東西設置爲2。 thread1能夠從內部創建新的thread2嗎?我覺得我會問,因爲我有一種習慣,當我嘗試做一些我不完全理解的事情時,會搞亂我的代碼。

+0

從技術上講,線程可以創建另一個線程線程......這一直髮生......你的應用程序以一個主線程開始...... – Yahia

+0

如果你已經測試過了,你會得到答案 – Mzf

+0

「繼續」可能不是C#中最好的變量名稱,因爲它也是一個關鍵詞。 – Inisheer

回答

3

是的,線程可以創建其他線程。 請記住,程序加載的「默認單線程」只是另一個正常的線程,所以當您啓動線程時,您已經從線程創建了新線程1

+0

哦,好吧,我對這樣的事情還是比較新的,我幾天前纔開始編程。感謝您及時的回覆! – ApachePilotMPE

相關問題