多线程并发执行并发函数的时候,当带多个参数的时候,当前.net并不支持
- 不带参数
并发要执行的函数或对象为run()
Thread[] threadarray = new Thread[usercount];
for (int w = 0; w < usercount; w++)
{
ThreadStart ts = new ThreadStart(run);
threadarray[w] = new Thread(ts);
}
for (int w = 0; w < usercount; w++)
{
threadarray[w].Start();
}
for (int w = 0; w < usercount; w++)
{
if (threadarray[w].IsAlive == true)
{
threadarray[w].Join();
}
}
2.带一个参数
并发要执行的函数或对象为run(),ParameterizedThreadStart 为.NET 2.0新增
ParameterizedThreadStart [] threadarray = new ParameterizedThreadStart [usercount];
for (int w = 0; w < usercount; w++)
{
ThreadStart ts = new ThreadStart(run);
threadarray[w] = new ParameterizedThreadStart (ts);
}
for (int w = 0; w < usercount; w++)
{
threadarray[w].Start(w);
}
for (int w = 0; w < usercount; w++)
{
if (threadarray[w].IsAlive == true)
{
threadarray[w].Join();
}
}
3.带2个以及2个以上参数
并发要执行的类为run(),用构造函数的方式实现多个参数的传递
Thread[] threadarray = new Thread[usercount];
for (int w = 0; w < usercount; w++)
{
run run1 = new run(s, filename, filecount);
ThreadStart ts = new ThreadStart(run1.run_update);
threadarray[w] = new Thread(ts);
}
for (int w = 0; w < usercount; w++)
{
threadarray[w].Start();
}
for (int w = 0; w < usercount; w++)
{
if (threadarray[w].IsAlive == true)
{
threadarray[w].Join();
}
}