Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether the thread is running or not in c#

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?

like image 586
Jose Ramon Avatar asked Oct 19 '22 21:10

Jose Ramon


1 Answers

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 ....
            }
        }
    }
like image 109
Irshad Avatar answered Oct 30 '22 04:10

Irshad