先定一個類,既有構造函數又有類方法:多線程
public class WriteNumber { /// <summary> /// 構造函數 /// </summary> public WriteNumber() { Console.WriteLine("WriteNumber-----begin"); Console.WriteLine("Name:" + Thread.CurrentThread.Name); Console.WriteLine("WriteNumber-----end"); } /// <summary> /// 類方法 /// </summary> public void WriteString() { Console.WriteLine("WriteString-----begin"); Console.WriteLine("Name:" + Thread.CurrentThread.Name); Console.WriteLine("WriteString-----end"); } }
用多線程運行:函數
public static void Main(string[] args) { try { Thread.CurrentThread.Name = "main"; //定義當前線程名稱 WriteNumber writenumber = new WriteNumber(); Thread t1 = new Thread(new ThreadStart(writenumber.WriteString)); t1.Name = "000"; //定義t1線程名稱 t1.Start(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); }
得出的結果:spa