static void Main(string[] args) { Thread thread = Thread.CurrentThread; thread.Name = "Main Thread"; string threadMessage = string.Format("Thread ID:{0} Current AppDomainId:{1} " + "Current ContextId:{2} Thread Name:{3} " + "Thread State:{4} Thread Priority:{5} ", thread.ManagedThreadId, Thread.GetDomainID(), Thread.CurrentContext.ContextID, thread.Name, thread.ThreadState, thread.Priority); Console.WriteLine(threadMessage); Console.WriteLine("Main threadId is:" + Thread.CurrentThread.ManagedThreadId); Message message = new Message(); Thread thread1 = new Thread(new ThreadStart(message.ShowMessage)); thread1.Start(); Console.WriteLine("Do something ..........!"); Console.WriteLine("Main thread working is complete!"); Console.ReadKey(); }
新建Message类:
class Message { public void ShowMessage() { string message = string.Format("Async threadId is :{0}",Thread.CurrentThread.ManagedThreadId); Console.WriteLine(message); for (int n = 0; n < 10; n++) { Thread.Sleep(300); Console.WriteLine("The number is:" + n.ToString()); } } }
结果为: