private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 15; i++)
{
Thread nova = new Thread(Method);
nova.Start();
}
listBox1.Items.Add("Some text");
}
private void Method()
{
for (int i = 0; i < 15; i++)
{
Console.WriteLine(i);
}
}这段代码写道:一些文本,然后是数字111222333.....我希望它写成111122223333....然后在最后写一些文字。有没有可能用线程(父线程等待子线程)来实现?或者我必须使用其他东西?
发布于 2012-06-23 00:10:29
您需要跟踪所有线程,并在每个线程上使用Thread.Join。它会一直等到指定的线程终止,然后继续执行。如下所示:
var threads = new List<Thread>();
for (int i = 0; i < 15; i++)
{
Thread nova = new Thread(Method);
nova.Start();
threads.Add(nova);
}
foreach (var thread in threads)
thread.Join();
listBox1.Items.Add("Some text");https://stackoverflow.com/questions/11160021
复制相似问题