C#多线程的参数传递
用C#创建线程是一件很方便的事,一个Void类型的函数,一句声明就可以创建出自己的线程应用。看下面的例子:
public class ThreadController
{
public ThreadController()
{
System.Threading.Thread thread = new System.Threading.Thread(ThreadMethod);
thread.Start();
}
private void ThreadMethod()
{
//Do something
}
}
{
public ThreadController()
{
System.Threading.Thread thread = new System.Threading.Thread(ThreadMethod);
thread.Start();
}
private void ThreadMethod()
{
//Do something
}
}
不过从上面的例子可以看到一个问题,就是ThreadMethod方法是不能传入参数的。要解决这个问题,需要用到ParameterizedThreadStart。看这个例子:
public class ThreadController
{
public ThreadController()
{
int value = 10;
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ThreadMethod));
thread.Start(value);
}
private void ThreadMethod(object parameter)
{
Console.WriteLine(parameter.ToString());
}
}
{
public ThreadController()
{
int value = 10;
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ThreadMethod));
thread.Start(value);
}
private void ThreadMethod(object parameter)
{
Console.WriteLine(parameter.ToString());
}
}
来自:https://www.cnblogs.com/guan345/archive/2010/05/07/1716412.html