1 using System; 2 using System.Threading; 3 4 //多线程调试: 2013.10.08 5 namespace ThreadExample 6 { 7 class App 8 { 9 public struct Data 10 { 11 public string Message; 12 } 13 14 //将消息写入控制台 15 static void ThreadMainWithParameter(object o) 16 { 17 Data d = (Data)o; 18 Console.WriteLine("Running in a thread , receive {0}.", 19 d.Message); 20 } 21 22 public static void Main() 23 { 24 var t1 = new Thread(() => Console.WriteLine("Running in a thread, id:{0}", 25 Thread.CurrentThread.ManagedThreadId)); 26 t1.Start(); 27 Console.WriteLine("This is the main thread, id:{0}", 28 Thread.CurrentThread.ManagedThreadId); 29 30 var d = new Data { Message = "Info"}; 31 var t2 = new Thread(ThreadMainWithParameter); 32 33 //传递变量d 34 t2.Start(d); 35 } 36 37 static void ThreadMain() 38 { 39 for (int i = 0; i < 100;i++ ) 40 { 41 Console.WriteLine("Running in a thread {0}.",i); 42 } 43 } 44 } 45 }
输出结果: