在c#中检查线程是否正在运行

时间:2015-12-03 10:34:19

标签: c# multithreading

我在c#代码中创建了一个名为ZipFolders的函数。事实上,我是从Unity按钮调用它,当它按下时尝试在目录中压缩文件夹。因为在同一时间我想做其他事情,我试图在新线程中调用该函数。我的问题是如何检查该线程是否正在运行或已停止。我的代码

onGUI():

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

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

我希望在更新函数中检查线程每次运行或已停止。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

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 ....
            }
        }
    }
相关问题