I am using loop to start multiple threads which will execute my method "ThreadFunc". I am giving each thread a name.
In my method "ThreadFunc", how to know which thread (thread name) is running my method?
My Method:
static void ThreadFunc()
{
lock (oLock)
{ //some work
}
Loop Starting Threads:
static Dictionary<string, Thread> ThreadsCollection = new Dictionary<string, Thread>();
foreach (string s in AllFiles)
{
Thread thread = new Thread(new ThreadStart(ThreadFunc));
thread.Name = s.Substring(s.IndexOf("doc"));
thread.Start();
ThreadsCollection.Add(thread.Name, thread);
}
In my method "ThreadFunc", how to know which thread (thread name) is running my method?
Get at the current thread with Thread.CurrentThread
, and then just use the Name
property:
string currentThreadName = Thread.CurrentThread.Name;
See more on this question at Stackoverflow