检测线程已经用C#.net运行? [英] Detecting a Thread is already running in C# .net?

查看:121
本文介绍了检测线程已经用C#.net运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code。

I am using following code.

public void runThread(){
    if (System.Diagnostics.Process.GetProcessesByName("myThread").Length == 0)
    {
    Thread t = new Thread(new ThreadStart(go));
    t.IsBackground = true;
    t.Name = "myThread";
    t.Start();
    }
    else
    {
      System.Diagnostics.Debug.WriteLine("myThreadis already Running.");
    }   
}
public void go()
{
    //My work goes here
}

我打电话runThread()函数很多时间,但我想主题仅当线程没有运行启动。这怎么可能?

I am calling runThread() function many time but i want thread only start when thread is not running. How is it possible?

推荐答案

GetProcessesByName不会看在你的应用程序线程,但在你的机器的进程。其实没有什么好办法让查询在自己的应用程序的线程(的事放在一边正在写一个调试器)。

GetProcessesByName will not look for threads in your application but for processes in your machine. In fact there is no good way to get query for the threads in your own application (a matter aside is writing a debugger).

有关你愿意,你可以创建一个包装类的线程以这种方式,你可以查询,如果他们是什么运行。或者保持线程跟踪自己通过其他方式

For what you want you could create a wrapper class for your threads in such way that you could query if they are running. Or keep track of the threads yourself by other means.

<打击>你也可以考虑有一个延迟&LT;主题&GT; 字段需要时将被初始化,您可以查询,看是否该线程,直到活着测试后延迟&LT;主题&GT; 是不是一个好主意。

You could also consider to have a Lazy<Thread> field that will be initialized when needed, and you can query to see if the thread is till alive. After testing Lazy<Thread> is not a good idea.

西蒙的回答衍生:

private int running;

public void runThread()
{
    if (Interlocked.CompareExchange(ref running, 1, 0) == 0)
    {
        Thread t = new Thread
        (
            () =>
            {
                try
                {
                    go();
                }
                catch
                {
                    //Without the catch any exceptions will be unhandled
                    //(Maybe that's what you want, maybe not*)
                }
                finally
                {
                    //Regardless of exceptions, we need this to happen:
                    running = 0;
                }
            }
        );
        t.IsBackground = true;
        t.Name = "myThread";
        t.Start();
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("myThreadis already Running.");
    }   
}

public void go()
{
    //My work goes here
}

*:得catch'em 所有

Wajid Segey 是正确的。你可以只拥有一个主题领域。请允许我提供的例子:

Wajid and Segey are right. You could just have a Thread field. Allow me to provide the example:

private Thread _thread;

public void runThread()
{
    var thread = _thread;
    //Prevent optimization from not using the local variable
    Thread.MemoryBarrier();
    if
    (
        thread == null ||
        thread.ThreadState == System.Threading.ThreadState.Stopped
    )
    {
        var newThread = new Thread(go);
        newThread.IsBackground = true;
        newThread.Name = "myThread";
        newThread.Start();
        //Prevent optimization from setting the field before calling Start
        Thread.MemoryBarrier();
        _thread = newThread;
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("myThreadis already Running.");
    }
}

public void go()
{
    //My work goes here
}

注意:这是更好地使用第一种选择(一西蒙的回答而来),因为它是线程安全的。也就是说,如果有各种线程调用runThread同时有正在创建多个线程的任何风险的方法

Note: It is better to use the first alternative (the one derived from Simon's answer) because it is thread-safe. That is, if there are various thread calling the method runThread simultaneously there is no risk of more than one thread being created.

这篇关于检测线程已经用C#.net运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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