- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace ForegroundThreadsAndThreadIDs
- {
- class Program
- {
- static void Main(string[] args)
- {
- for (int i = 0; i < Environment.ProcessorCount; i++)
- {
- Thread thread = new Thread(DefferentThread);
- Thread.Sleep(1000);
- thread.Start(i);
- }
- Console.WriteLine("This is MainThread");
- }
- static void DefferentThread(object threadID)
- {
- while (true)
- {
- Thread.Sleep(1000);
- Console.WriteLine("This is DefferentThread:{0}-{1}", threadID, Thread.CurrentThread.ManagedThreadId);
- }
- }
- }
- }
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