2014-01-22 95 views
0

我正在使用循環啓動多個線程,這將執行我的方法「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); 
     } 

回答

4

在我的方法 「的ThreadFunc」,怎麼知道哪個線程(線程名)正在運行我的方法?

在獲取與Thread.CurrentThread當前線程,然後只需使用Name屬性:

string currentThreadName = Thread.CurrentThread.Name; 
+0

@ನಿಶಿತ್:請不要添加註釋無關的答案。這基本上是垃圾評論。 –

+0

是的,我理解並刪除了它。 – 2014-01-22 11:11:28

相關問題