Reputation: 553
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.
Upvotes: 2
Views: 293
Reputation: 116
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);
}
}
}
Upvotes: 1