STAThread
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Threading; namespace ThreadApp { class Program { private static int item = 0; [STAThread]//该特性一般用来标志程序的入口函数,对于一般的方法设置没有任何的影响,如下面输出的结果 private static void DisplayData() { item++; string name=null; name=Thread.CurrentThread.Name; Console.WriteLine("The name of the running thread is:"+name+"\titem:"+item.ToString()); Console.WriteLine(Thread.CurrentThread.ApartmentState.ToString());//输出MTA,ApartmentState用来设置线程的单元状态 } [STAThread] static void Main(string[] args) { Thread t1 = new Thread(DisplayData); t1.Name = "First Thread"; t1.Start(); Thread t2 = new Thread(DisplayData); t2.Name = "Second Thread"; t2.Start(); Console.WriteLine(Thread.CurrentThread.ApartmentState.ToString());//输出的值由主函数Main上的特性来决定 Console.Read(); } } }