public Thread(ParameterizedThreadStart start)
ParameterizedThreadStart 委托表示线程开始执行时要调用的方法。
线程在创建时不会开始执行。所要为执行而调度线程,请调用Start() 方法,若要将数据对象传递给线程,请调用Start(Object)重载
代码示例演示通过静态方法和示例方法创建和使用ParameterizedThreadStart委托。
using System;
using System.Threading;
class ThreadTest
{
static void Main()
{
Thread thread = new Thread(DoWork);
thread.Start(10);
ThreadTest test = new ThreadTest();
thread = new Thread(test.DoMoreWork);
thread.Start("The answer. ");
}
static public void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data = {0}", data);
}
public void DoMoreWork(object data)
{
Console.WriteLine("Instance thread procedure. Data = {0}", data);
}
}
public Thread(ThreadStart start)
System.Threading.ThreadStart委托,它表示此线程开始执行时调用的方法。
若要调度线程,需要执行Start()方法。
using System;
using System.Threading;
class Test
{
static void Main()
{
Thread newThread = new Thread(new ThreadStart(Work.DoWork));
newThread.Start();
Work work = new Work();
newThread = new Thread(new ThreadStart(work.DoMoreWork));
newThread.Start();
}
}
class Work
{
Work();
static void DoWork()
{
}
public void DoMoreWork()
{
}
}
可以看出以上两个Thread构造方法不存在差别。
Thread thread = new Thread(aMethod);
Thread thread = new Thread(new ThreadStart(aMethod));
这两种构造方式是等价的。
另外两个构造函数:
public Thread(ParameterizedThreadStart start,int maxStackSize)
maxStackSize线程要使用的最大堆栈大小;如果为 0 则使用可执行文件的文件头中指定的默认最大堆栈大小。
public Thread(ThreadStart start,int maxStackSize)