Foreground Threads And Thread IDs in C#

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Threading;  
  7. namespace ForegroundThreadsAndThreadIDs  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             for (int i = 0; i < Environment.ProcessorCount; i++)  
  14.             {  
  15.                 Thread thread = new Thread(DefferentThread);  
  16.                 Thread.Sleep(1000);  
  17.                 thread.Start(i);  
  18.             }  
  19.             Console.WriteLine("This is MainThread");  
  20.         }  
  21.         static void DefferentThread(object threadID)  
  22.         {  
  23.             while (true)  
  24.             {  
  25.                 Thread.Sleep(1000);  
  26.                 Console.WriteLine("This is DefferentThread:{0}-{1}", threadID, Thread.CurrentThread.ManagedThreadId);  
  27.             }  
  28.         }  
  29.     }  
  30. }  
Output:
 
This is DefferentThread:0-3
This is DefferentThread:1-4
This is DefferentThread:0-3
This is DefferentThread:1-4
This is DefferentThread:2-5
This is MainThread
This is DefferentThread:0-3
This is DefferentThread:3-6
This is DefferentThread:1-4
This is DefferentThread:1-4
This is DefferentThread:3-6
This is DefferentThread:2-5
This is DefferentThread:0-3