2

If you've done something like

Thread logSend = new Thread(() => senddat.SendLoggedData());
logSend.Priority = ThreadPriority.AboveNormal;
logSend.Name = "BackLogThread";
logSend.IsBackground = false;
logSend.Start();

Are you able to simply verify at a later point, from another thread, that there IS a thread with the name "BackLogThread" currently running?

I don't want to do anything to the thread, I just want to query the threads inside my application to see if there is one with that particular name.

Edit:

As far as the other question that was asked related to this one, that solution is viable, but it is a little much for what I am asking. The person in that example wants to know a lot about the thread like their priority etc. for logging information. I want at most a true to return if there's any thread anywhere with the name "BackLogThread" running.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
Justin
  • 533
  • 1
  • 7
  • 18
  • 7
    You can just store references to the threads you create. – Brian Rasmussen Oct 30 '15 at 16:15
  • Possible duplicate of [Getting list of currently active managed threads in .NET?](http://stackoverflow.com/questions/1825882/getting-list-of-currently-active-managed-threads-in-net) – Ron Beyer Oct 30 '15 at 16:21
  • I was hoping for a less complicated solution than having a class for my threads or something like that. .NET really doesn't have anything that can return a list of currently active threads? – Justin Oct 30 '15 at 16:26

1 Answers1

1

As far as I know this is not possible because there's no way to get all managed threads in C#. But you can manually add them to a pool and retrieve them later.

The pool/registry can look like this:

public class ThreadRegistry
{
   private static ThreadRegistry instance;
   private readonly object syncRoot = new object();
   private readonly IDictionary<string, Thread> threads = new Dictionary<string, Thread>();

   private ThreadRegistry()
   {
   }

   public static ThreadRegistry Instance => instance ?? (instance = new ThreadRegistry());

   public Thread this[string name]
   {
      get
      {
         lock (this.syncRoot)
         {
            return this.threads[name];
         }
      }
   }

   public void Register(Thread thread)
   {
      lock (this.syncRoot)
      {
         this.threads.Add(thread.Name, thread);
      }
   }

   public bool ThreadExists(string name)
   {
      lock(this.syncRoot)
      {
         return this.threads.ContainsKey(name);
      }
   }
}
Bruno Leupi
  • 116
  • 4
  • The only change I would suggest is because Threads are "global things" make the entire class static. – Scott Chamberlain Oct 30 '15 at 16:52
  • @ScottChamberlain Yes, could be a solution. But limits the class to use if you want more than one registry. The other thing is, that you have to use a static getter instead of an indexer. – Bruno Leupi Oct 30 '15 at 16:58
  • This is a good solution but I'll opt for something even simpler I think, either a static bool sitting somewhere or even creating an empty file and checking if it exists instead of launching another thread, as it doesn't happen very often anyway. – Justin Oct 30 '15 at 18:57
  • What about a singleton and an additional method? – Bruno Leupi Oct 30 '15 at 19:30