c# 可以动态创建childThread嘛?并且join 参考using System.Threading; Thread thread1 = new Thread(new ThreadStart(setDatagridview11)); Thread thread2 = new Thread(new ThreadStart(setDatagridview22)); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join();
时间: 2023-08-16 10:03:38 浏览: 151
是的,C#也可以使用类似的方式来动态创建子线程并等待其结束。以下是一个类似的C#示例代码:
```csharp
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Thread thread1 = new Thread(new ThreadStart(setDatagridview11));
Thread thread2 = new Thread(new ThreadStart(setDatagridview22));
thread1.Start();
thread2.Start();
// 等待所有子线程结束
thread1.Join();
thread2.Join();
Console.WriteLine("All threads finished");
}
static void setDatagridview11()
{
Console.WriteLine("Thread 1 started");
// do some work
Console.WriteLine("Thread 1 finished");
}
static void setDatagridview22()
{
Console.WriteLine("Thread 2 started");
// do some work
Console.WriteLine("Thread 2 finished");
}
}
```
这段代码会创建两个子线程,分别执行setDatagridview11()和setDatagridview22()方法。主线程会等待所有子线程执行完毕后才会输出"All threads finished"。
阅读全文