Is Thread Detection already running in C # .net?

I am using the 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 call the runThread () function many times, but I want the thread to start only when the thread is not running. How is this possible?

+3
c #
source share
5 answers

GetProcessesByName will not search for threads in your application, but for processes on your computer. Actually there is no good way to get a request for threads in your own application (the question aside is the debugger).

Why can you create a wrapper class for your threads so that you can query if they are running. Or track the topics themselves in other ways .

You can also think about having a Lazy<Thread> field, which will be initialized if necessary, and you can ask if the thread will burn out. After testing, Lazy<Thread> not a good idea.


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

*: Gotta catch'em all


Wajid and Segey are right. You may have a Thread field. Let me give an 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 } 

Note Itโ€™s better to use the first alternative (obtained from Simonโ€™s answer), since it is thread safe. That is, if there are several threads calling the runThread method at the same time, there is no risk of creating more than one thread.

+6
source share

One simple way is that you can have a flag indicating whether it works or not. You may need to use some lock if these are conflicts.

 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; } 
+2
source share

You can use Thread.IsAlive to check if the prevoius thread is working or not. This should provide thread status. You can put this check before mythread.Start().

+1
source share

Do you only create a thread in the run thread method? If he holds it like that, as a class field that contains the runThread method and requests t.IsAlive.

0
source share

Maybe this will 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 } 
0
source share

All Articles