c# -线程中的方法没有被执行
本文关键字:执行 方法 线程 | 更新日期: 2023-09-27 18:13:20
线程中的方法未执行,使用thread.start();相反,当我使用thread.join()时,方法被执行。
Thread[] threads = new Thread[12];
int temp;
//_stopRequest = false;
for (int i = 0; i < threads.Length - 1; i++)
{
temp = i;
threads[temp] = new Thread(new ThreadStart(() => test(test1[temp],"start", temp)));
threads[temp].Start();
//threads[temp].Join();
}
有人能解释一下吗?
尝试同时启动所有线程
尝试添加
for(int i=0; i<threads.Length-1; i++)
{
threads[i].Join();
}
在方法的末尾。你的线程将同时启动,你将等待它们的结束。其他变体:设置它们的IsBackground属性为true。所以,直到这些线程完成它们的工作,你的程序才会结束。
注:在循环中使用"int temp = i;",如果你想使用闭包。在您的例子中,所有线程都闭包到同一个变量temp.