检测线程是否已经在 C# .net 中运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12949024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting a Thread is already running in C# .net?
提问by RAKESH HOLKAR
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
}
I am calling runThread() function many time but i want thread only start when thread is not running. How is it possible?
我多次调用 runThread() 函数,但我希望线程仅在线程未运行时启动。这怎么可能?
采纳答案by Theraot
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).
GetProcessesByName 不会在您的应用程序中寻找线程,而是在您的机器中寻找进程。事实上,没有什么好方法可以查询您自己的应用程序中的线程(另外一个问题是编写调试器)。
For what you want you could create a wrapper class for your threadsin such way that you could query if they are running. Or keep track of the threads yourself by other means.
对于你想要的,你可以为你的线程创建一个包装类,这样你就可以查询它们是否正在运行。或者通过其他方式自己跟踪线程。
You could also consider to have a After testing Lazy<Thread>field that will be initialized when needed, and you can query to see if the thread is till alive.Lazy<Thread>is not a good idea.
您还可以考虑设置一个经过测试Lazy<Thread>在需要时初始化的字段,您可以查询该线程是否一直处于活动状态。Lazy<Thread>不是一个好主意。
Derived from Simon's answer:
源自西蒙的回答:
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
}
Wajidand Segeyare right. You could just have a Thread field. Allow me to provide the example:
Wajid和Segey是正确的。你可以只有一个 Thread 字段。请允许我提供示例:
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
}
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.
注意:最好使用第一个替代方案(源自 Simon 的答案),因为它是线程安全的。也就是说,如果有多个线程同时调用 runThread 方法,就不会有创建多个线程的风险。
回答by Simon Edstr?m
One easy way is that you could have a flag that indicates if it's running or not. You maybe have to use some lockif it's some conflicts.
一种简单的方法是您可以有一个标志来指示它是否正在运行。lock如果有一些冲突,您可能必须使用一些。
public static bool isThreadRunning = false;
public void runThread()
{
if (!isThreadRunning)
{
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()
{
isThreadRunning = true;
//My work goes here
isThreadRunning = false;
}
回答by Simon Edstr?m
You can use Thread.IsAliveto check whether prevoius thread is running or not.This is to give the thread status.You can put this check before mythread.Start().
你可以Thread.IsAlive用来检查prevoius线程是否正在运行。这是给线程状态。你可以把这个检查放在mythread.Start().
回答by Sergey Kucher
Do you create the thread only in run thread method? If it is so hold it as field of the class that holds runThread method and ask t.IsAlive.
您是否仅在运行线程方法中创建线程?如果是这样,则将其作为持有 runThread 方法的类的字段并询问 t.IsAlive。
回答by BD Magic
Maybe this can help you
也许这可以帮助你
static bool isRunning = false;
public void RunThread(){
if (!isRunning)
{
Thread t = new Thread(()=> { go(); isRunning = true;});
t.IsBackground = true;
t.Name = "myThread";
t.Start();
}
else
{
System.Diagnostics.Debug.WriteLine("myThread is already Running.");
}
}
public void go()
{
//My work goes here
}

