0

EDIT: Question answered. Answer explained perfectly by Igor. (Thank you!)

Question: How do I access/control a thread from another class in the same program? I'm will be having multiple threads active (not all at once) and I need to check if one is active (that is not the main thread).

I am programming in C# and I am trying to use threading. I have 2 classes, my thread starts in the main class calling a function in the other class. In my other class I want to see if "thread.isAlive == true" but I think it isn't publicly available. I don't know the syntax/code to be able to use the thread from another class? I'm struggling to get it working.

I can call the other class, but I can't call the thread between classes. (Can't declare a thread outside of a class) The error thrown is:

Error   1   The name 'testThread' does not exist in the current context

Example code:

//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

Thanks for reading! -Dave

Hughsie28
  • 103
  • 2
  • 13
  • 2
    TL;DR -- why don't you just adjust the access modifiers so it's accessible outside the class? – rory.ap Apr 02 '15 at 13:09
  • 1
    Pass the thread into the `TestClass`'s constructor. –  Apr 02 '15 at 13:10
  • 1
    You can pass testThread from your main class to the runFunction method of your other class. See this SO answer as an example, http://stackoverflow.com/questions/3360555/how-to-pass-parameters-to-threadstart-method-in-thread – Shar1er80 Apr 02 '15 at 13:12
  • Do you mean the Thread.IsAlive property? I think that will *always* be true if you are inside runFunction. I don't understand how a thread could ask if itself is alive. – Moby Disk Apr 02 '15 at 13:12

1 Answers1

5
if (System.Threading.Thread.CurrentThread.isAlive == true) { ... }

But you are doing this: "Is the thread, in which I am executing, running? Yes it is running since the code that is doing the check is in it and I am currently in that code."

But if you insist:

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
Igor
  • 15,833
  • 1
  • 27
  • 32
  • That is close to what I'm after; but I'm trying to check if a thread I have created that runs parallel to the main thread is active or not – Hughsie28 Apr 02 '15 at 13:17
  • Your checking code is running in the thread whose running status you are trying to check. Guess what the result of the check will be. – Igor Apr 02 '15 at 13:20
  • I will be having multiple threads running in the full program and I need to find out if one of them will be running or not as they won't always be active at once – Hughsie28 Apr 02 '15 at 13:21
  • Ah I see where you are coming from now. That makes much more sense! – Hughsie28 Apr 02 '15 at 13:26