如何知道哪个线程正在运行我的方法?

时间:2014-01-22 10:58:24

标签: c# multithreading

我正在使用循环来启动多个线程,这将执行我的方法“ThreadFunc”。  我给每个帖子一个名字。

在我的方法“ThreadFunc”中,如何知道哪个线程(线程名称)正在运行我的方法?

我的方法:

 static void ThreadFunc()
    {
        lock (oLock)
        { //some work
        }

循环启动主题:

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);
        }

1 个答案:

答案 0 :(得分:4)

  

在我的方法“ThreadFunc”中,如何知道哪个线程(线程名称)正在运行我的方法?

使用Thread.CurrentThread获取当前主题,然后使用Name属性:

string currentThreadName = Thread.CurrentThread.Name;