14

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!

Jacob
  • 77,566
  • 24
  • 149
  • 228
Petteri
  • 221
  • 3
  • 10
  • Just to add a small note to the answers below, it may be wise in some situations to use WeakReference to prevent memory leaks. Threads can have more data associated with them than most realize since libraries can elect to use things like TLS/CallContext. If you're not planning to explicitly remove your threads from the collection when they end the WeakReference will still allow garbage collection. – TheXenocide May 11 '12 at 14:53

4 Answers4

4

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • This is exactly what I do now and it works fine. I was hoping there would be a better(?) way of doing it! – Petteri Jan 09 '09 at 13:46
  • Do not know why it should be usefull to have a dictionary, if he wishes just Enumeration, but thats not the point ;-). It is usefull to get an overview of what is going on, to monitor the thrading-scaling of the app. Not for communicating between threads. – Oliver Friedrich Jan 09 '09 at 14:23
0

Note also that thread names are not required to be unique. Seems like using the thread ID might be a better option...

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
-1

So, after my mistake with the process threads, here a way how to hold your Threads. Nothing spectacular, but I think coding examples are very handy anytime.

List<Thread> threads = new List<Thread>();

for (int i = 0; i < 10; i++)
{

    Thread t = new Thread(delegate()
        {
        do
        {
            Thread.Sleep(50);
        } while (true);
        });

    t.IsBackground = true;
    t.Name = i.ToString();
    t.Start();
    threads.Add(t);
}

foreach (Thread t in threads)
{
    Console.WriteLine(t.Name);
}
Oliver Friedrich
  • 9,018
  • 9
  • 42
  • 48
  • If I understand your code correctly, the code will store each thread in a list, pretty much like Marc Gravell suggested. I store the threads in a Dictionary. – Petteri Jan 09 '09 at 13:45
  • Yes, if it has any benefit, that you use a dictionary, use it. But for my democode a list was correct. – Oliver Friedrich Jan 09 '09 at 14:26
-2

I think you want the following

System.Diagnostics.Process.GetCurrentProcess().Threads

Michael Prewecki
  • 2,064
  • 4
  • 16
  • 28
  • Combined with some kind of search (foreach, LINQ statements?) on the resulting collection to get the Thread with the name you want. Beware that Thread names need not be unique, as other replies have mentioned. – peSHIr Jan 09 '09 at 13:12
  • Sorry, but did the same mistake, that collection return not Threads but ProcessThreads, so not the ones, he starts in his code. – Oliver Friedrich Jan 09 '09 at 14:24