本來自於https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.threadstart?view=netframework-4.8;
static void Main(string[] args)
{
ThreadStart threadStartDelegate = new ThreadStart(Work.DoWork); //創建委托
Thread thread = new Thread(threadStartDelegate); //用ThreadStart委托實例化線程Thread
thread.Start();
{
ThreadStart threadStartDelegate = new ThreadStart(Work.DoWork); //創建委托
Thread thread = new Thread(threadStartDelegate); //用ThreadStart委托實例化線程Thread
thread.Start();
Work work = new Work();
work.Data = 42;
threadStartDelegate = new ThreadStart(work.DoMoreWork);
thread = new Thread(threadStartDelegate);
thread.Start();
Console.ReadKey();
}
work.Data = 42;
threadStartDelegate = new ThreadStart(work.DoMoreWork);
thread = new Thread(threadStartDelegate);
thread.Start();
Console.ReadKey();
}
public class Work
{
public static void DoWork()
{
Console.WriteLine("Static thread procedure.");
}
public int Data;
public void DoMoreWork()
{
Console.WriteLine("Instance thread procedure. Data={0}", Data);
}
}
{
public static void DoWork()
{
Console.WriteLine("Static thread procedure.");
}
public int Data;
public void DoMoreWork()
{
Console.WriteLine("Instance thread procedure. Data={0}", Data);
}
}
C#使用線程時首先需要創建線程,使用Thread類構造函數創建實例需要用到ThreadStart委托或者ParameterizedThreadStart 委托創建 Thread 類的實例,ThreadStart 委托只能用於無返回值、無參數的方法,ParameterizedThreadStart 委托則可以用於帶參數的方法。線程不會直接運行,直至調用Start()方法時為止。