如果启动一个带参数的线程:
新建一个控制台项目ThreadDemo;
将需要携带的参数封装在一个类中,然后在写一个带参构造函数;
最后调用,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadDemo
{
class Program
{
static void Main(string[] args)
{
ThreadWithParam t = new ThreadWithParam("测试带参数启动新Thread。");
Thread thread = new Thread(new ThreadStart(t.Printout));
thread.Start();
Console.ReadLine();
}
}
class ThreadWithParam
{
private string param;
public ThreadWithParam(string param)
{
this.param = param;
}
public void Printout()
{
Console.WriteLine(param);
}
}
}