2013-06-05 175 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

相关问题