5

I have created a function in c# code which called ZipFolders. In fact I am calling it from a Unity button and when it pressed try to zip folders inside a dir. Since in the same time I wanted to do something else I tried to call that function in a new thread. My question is how can I check if that thread is running or has stopped. My code

onGUI():

if (GUI.Button (new Rect (390, 250, 100, 50), "ZIP_FILES")) {

        Thread thread = new Thread(new ThreadStart(zipFile));
        thread.Start();
}

I want in an update function to check every time fi the thread is running or has stopped. How can I do so?

Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
  • 6
    Possible duplicate of [Thread.IsAlive and Thread.ThreadState==ThreadState.Running](http://stackoverflow.com/questions/16232521/thread-isalive-and-thread-threadstate-threadstate-running) – Henrik Dec 03 '15 at 10:36

1 Answers1

3

You can use thread.ThreadState property

EDIT:

You can do like this;

public class YourClass 
    {
        private Thread _thread;

        private void YourMethod() 
        {
            if (GUI.Button (new Rect (390, 250, 100, 50), "ZIP_FILES")) {

                _thread = new Thread(new ThreadStart(zipFile));
                _thread.Start();
            }            
        }

        private void YourAnotherMethod() 
        {
            if (_thread.ThreadState.Equals(ThreadState.Running)) 
            {
                //Do ....
            }
        }
    }
Irshad
  • 3,071
  • 5
  • 30
  • 51
  • How can I check the thread state in another block of code?From where should I call thread state in order to check it all the time? – Jose Ramon Dec 03 '15 at 10:42
  • Make the `thread` a class variable and you can call it anywhere you want. – Irshad Dec 03 '15 at 10:43