Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate threads in .NET using the Name property?

Suppose I start two threads like this:

// Start first thread
Thread loaderThread1 = new Thread(loader.Load);
loaderThread1.Name = "Rope";
loaderThread1.Start();

// Start second thread
Thread loaderThread2 = new Thread(loader.Load);
loaderThread2.Name = "String";
loaderThread2.Start();

Is there any way I can enumerate the threads by using their Name property?

I want to be ablie to check if a thread with a specific name is still running.

Each thread I create works with a named set of data, the name of the data set is used as the name for the thread working with the data. Before starting a worker thread I want to see if any previous thread for the same set of data is already running.

The threads I get when using System.Diagnostics.GetCurrentProcess().Threads are of type ProcessThread, not Thread!

like image 890
Petteri Avatar asked Jan 09 '09 09:01

Petteri


1 Answers

I suspect you might have to put the threads into a Dictionary<string,Thread> for that to work - but why do you want it anyway? There are usually other ways of communicating between threads (any of the lock / wait objects).

To work at the process level (i.e. not thinking of the Thread object), see here - you could limit it to the current process, but you won't be able to interact with the thread.

like image 154
Marc Gravell Avatar answered Oct 05 '22 13:10

Marc Gravell