1

I have an application that has the ability to kick off a new thread that opens a new form (MonitorForm). When I create the thread, I give it a name (let's say, "MyAwesomeThread"). When the originating application tries to kick off the new thread to open the new form, I would like to be able to check if "MyAwesomeThread" is already running. From what I have read, I can only get the names of the processes that are running.

If I have a named thread, how can I check to see if that thread is already running using the name? Does the process inherit the name?

bool threadIsNotRunningAlready =  /** what goes here?? **/

if(threadIsNotRunningAlready)
{
    ThreadStart threadStart = () => GoGoAwesomeThread(param1, param2);

    var executionThread = new Thread(threadStart);
    executionThread.Name = "MyAwesomeThread";
    executionThread.Start();
}
gwin003
  • 7,432
  • 5
  • 38
  • 59
  • If you attach a debugger to the process, you can select "Break all" and then you will be able to look at all the threads and see their names (and their call stacks). Visual Studio will do this for you. – Matthew Watson Apr 26 '13 at 18:04
  • There is a difference between a thread and a process. Do you create a new thread and name it or a process and name it? – bash.d Apr 26 '13 at 18:06
  • @bash.d Looks like he's naming a thread: `executionThread.Name = "MyAwesomeThread"` – Matthew Watson Apr 26 '13 at 18:07
  • OP is talking about processes, too. I don't get it really... – bash.d Apr 26 '13 at 18:08
  • Correct, I do understand that a thread and a process are different, but how can I see if my thread (or process on that thread) is running? Does the process have the same name as the thread? – gwin003 Apr 26 '13 at 18:08
  • @gwin003 Do you need to call this from a different process than the one that created the thread? – Matthew Watson Apr 26 '13 at 18:10
  • Are you doing Process.Start() in this new thread? If so, you'll need to track that Process to see if its running. – bland Apr 26 '13 at 18:11
  • Matthew - No I can call this from the same thread everytime. bland - No I am never actually creating the Process or calling Process.Start(). The code above is all I am doing. – gwin003 Apr 26 '13 at 18:13
  • 2
    @gwin003 Seems like you can't do it. [Check out this other thread](http://stackoverflow.com/questions/1825882/getting-list-of-currently-active-managed-threads-in-net) - [and also this one](http://stackoverflow.com/questions/466799/how-can-i-enumerate-all-managed-threads-in-c) – Matthew Watson Apr 26 '13 at 18:16
  • Thanks Matt. Not exactly the answer I was hoping for, but guess I'll have to deal with it. – gwin003 Apr 26 '13 at 18:19

0 Answers0