从另一个类(C#)调用线程

从另一个类(C#)调用线程,c#,multithreading,class,C#,Multithreading,Class,编辑:问题已回答。答案由Igor完美解释。(谢谢!) 问题:如何访问/控制同一程序中另一类的线程? 我将有多个线程处于活动状态(不是一次全部),我需要检查一个线程是否处于活动状态(不是主线程) 我正在用C#编程,我正在尝试使用线程。 我有两个类,我的线程从主类开始调用另一个类中的函数。在我的另一个类中,我想看看“thread.isAlive==true”,但我认为它不是公开的。我不知道能够使用另一个类的线程的语法/代码?我正在努力让它工作 我可以调用另一个类,但不能调用类之间的线程。(无法在类外

编辑:问题已回答。答案由Igor完美解释。(谢谢!)

问题:如何访问/控制同一程序中另一类的线程? 我将有多个线程处于活动状态(不是一次全部),我需要检查一个线程是否处于活动状态(不是主线程)

我正在用C#编程,我正在尝试使用线程。 我有两个类,我的线程从主类开始调用另一个类中的函数。在我的另一个类中,我想看看“thread.isAlive==true”,但我认为它不是公开的。我不知道能够使用另一个类的线程的语法/代码?我正在努力让它工作

我可以调用另一个类,但不能调用类之间的线程。(无法在类外声明线程) 引发的错误是:

Error   1   The name 'testThread' does not exist in the current context
示例代码:

//Headers
using System.Threading;
using System.Threading.Tasks;
namespace testProgram
{
    public class Form1 : Form
    {
        public void main()
        {
            //Create thread referencing other class
            TestClass test = new TestClass();
            Thread testThread = new Thread(test.runFunction)
            //Start the thread
            testThread.Start();
        }//Main End
    }//Form1 Class End
    public class TestClass
    {
        public void runFunction()
        {
            //Check if the thread is active
            //This is what I'm struggling with
            if (testThread.isAlive == true)
            {
                //Do things
            }//If End
        }//runFunction End
    }//testClass End
}//Namespace End
谢谢你的阅读! -戴夫

但您正在这样做:“我正在执行的线程是否正在运行?是的,它正在运行,因为执行检查的代码在其中,而我当前在该代码中。”

但如果你坚持:

public class Form1 : Form
{
    public void main()
    {
        //Create thread referencing other class
        TestClass test = new TestClass();
        Thread testThread = new Thread(test.runFunction)
        test.TestThread = testThread;
        //Start the thread
        testThread.Start();
    }//Main End
}//Form1 Class End
public class TestClass
{
    public Thread TestThread { get; set; }
    public void runFunction()
    {
        //Check if the thread is active
        if (TestThread != null && TestThread.isAlive == true)
        {
            //Do things
        }//If End
    }//runFunction End
}//testClass End
但您正在这样做:“我正在执行的线程是否正在运行?是的,它正在运行,因为执行检查的代码在其中,而我当前在该代码中。”

但如果你坚持:

public class Form1 : Form
{
    public void main()
    {
        //Create thread referencing other class
        TestClass test = new TestClass();
        Thread testThread = new Thread(test.runFunction)
        test.TestThread = testThread;
        //Start the thread
        testThread.Start();
    }//Main End
}//Form1 Class End
public class TestClass
{
    public Thread TestThread { get; set; }
    public void runFunction()
    {
        //Check if the thread is active
        if (TestThread != null && TestThread.isAlive == true)
        {
            //Do things
        }//If End
    }//runFunction End
}//testClass End

TL;DR——为什么不调整访问修饰符,使其在类外可访问?将线程传递到
TestClass
的构造函数中。您可以将testThread从主类传递到另一个类的runFunction方法。以这个答案为例,您是指Thread.IsAlive属性吗?我认为,如果您在runFunction中,这将永远是正确的。我不明白一个线程怎么会问自己是否还活着;DR——为什么不调整访问修饰符,使其在类外可访问?将线程传递到
TestClass
的构造函数中。您可以将testThread从主类传递到另一个类的runFunction方法。以这个答案为例,您是指Thread.IsAlive属性吗?我认为,如果您在runFunction中,这将永远是正确的。我不明白一条线怎么会问自己是否还活着,这和我想要的差不多;但是我正在尝试检查我创建的与主线程并行运行的线程是否处于活动状态,或者您的检查代码是否正在您尝试检查其运行状态的线程中运行。猜猜检查的结果会是什么。我将在完整的程序中运行多个线程,我需要确定其中一个线程是否正在运行,因为它们不会一直处于活动状态。我知道你从现在开始。那就更有意义了!这接近于我所追求的;但是我正在尝试检查我创建的与主线程并行运行的线程是否处于活动状态,或者您的检查代码是否正在您尝试检查其运行状态的线程中运行。猜猜检查的结果会是什么。我将在完整的程序中运行多个线程,我需要确定其中一个线程是否正在运行,因为它们不会一直处于活动状态。我知道你从现在开始。那就更有意义了!