在C#中检查线程是否正在运行 [英] Check whether the thread is running or not in c#

查看:1903
本文介绍了在C#中检查线程是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

您可以使用thread.ThreadState属性

您可以这样做;

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 ....
            }
        }
    }

这篇关于在C#中检查线程是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆