## 无参数
我们都知道使用多线程最简单的方式是下面这样:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
void
SomeMethod()
{
// do something
}
void CreateThread()
{
Thread thread = new Thread( new ThreadStart(SomeMethod));
thread.Start();
}
{
// do something
}
void CreateThread()
{
Thread thread = new Thread( new ThreadStart(SomeMethod));
thread.Start();
}
##一个参数
因为Thread类并不支持这样
但是为了解决这个问题,类库中提供了一个支持的代理
ParameterizedThreadStart
使用它可以支持传入一个参数.注意,是仅仅一个!!
使用以下方法实现
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
void
SomeMethod(
object
arg)
{
// 假如传的是string
// string argStr = (string)arg;
// do something
}
void CreateThread()
{
Thread thread = new Thread( new ParameterizedThreadStart(SomeMethod));
string arg = " Hello World! " ;
thread.Start(arg);
}
{
// 假如传的是string
// string argStr = (string)arg;
// do something
}
void CreateThread()
{
Thread thread = new Thread( new ParameterizedThreadStart(SomeMethod));
string arg = " Hello World! " ;
thread.Start(arg);
}
##多个参数
但是假如有多个参数就遇到麻烦了,上述方法只允许一个参数.
我们有两种方法:
1. 新建一个内部类,包装所有的参数,然后传入这个内部类的一个实例,如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
void
SomeMethod(
object
myArgs)
{
MyArgs args = (MyArgs)myArgs;
string str = args.HelloString;
// do something
}
void CreateThread()
{
Thread thread = new Thread( new ParameterizedThreadStart(SomeMethod));
MyArgs args = new MyArgs( " Hello World! " )
thread.Start(args);
}
private class MyArgs
{
string helloString;
public string HelloString
{
get { return helloString; }
set { helloString = value; }
}
MyArgs( string helloString)
{
this .helloString = helloString;
}
}
{
MyArgs args = (MyArgs)myArgs;
string str = args.HelloString;
// do something
}
void CreateThread()
{
Thread thread = new Thread( new ParameterizedThreadStart(SomeMethod));
MyArgs args = new MyArgs( " Hello World! " )
thread.Start(args);
}
private class MyArgs
{
string helloString;
public string HelloString
{
get { return helloString; }
set { helloString = value; }
}
MyArgs( string helloString)
{
this .helloString = helloString;
}
}
2. 调用方法需要使用的参数存在此方法的类中,代码示例如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
class
MyThreadHelper {
// 要用到的属性,也就是我们要传递的参数
private string helloString;
public string HelloString
{
get { return helloString; }
set { helloString = value; }
}
// 包含参数的构造函数
public MyThreadHelper( string helloString)
{
this .helloString = helloString;
}
// 要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用
public void SomeMethod()
{
string helloString = this .helloString;
// do something
}
}
void CreateThread()
{
MyThreadHelper helper = new MyThreadHelper( " Hello World! " );
Thread thread = new Thread( new ThreadStart(helper.SomeMethod));
thread.Start();
}
// 要用到的属性,也就是我们要传递的参数
private string helloString;
public string HelloString
{
get { return helloString; }
set { helloString = value; }
}
// 包含参数的构造函数
public MyThreadHelper( string helloString)
{
this .helloString = helloString;
}
// 要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用
public void SomeMethod()
{
string helloString = this .helloString;
// do something
}
}
void CreateThread()
{
MyThreadHelper helper = new MyThreadHelper( " Hello World! " );
Thread thread = new Thread( new ThreadStart(helper.SomeMethod));
thread.Start();
}
3. 直接在ThreadStart构造函数中添加代理,这种方法不用考虑参数的个数 (感谢 qianlifeng)
代码如下:
ThreadStart starter
=
delegate
{ SomeMethod(arg1, arg2, arg3); };
new Thread(starter).Start();
new Thread(starter).Start();
##结束
至此,多线程传不同个数(零到多个)参数都有了解决办法.
老子曰: 道生一 一生二 二生三 三生万物