gpt4 book ai didi

c# - 请帮忙。创建线程并等待完成

转载 作者:太空宇宙 更新时间:2023-11-03 20:40:58 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
C# Spawn Multiple Threads for work then wait until all finished

我有两个方法调用,我想使用两个线程调用它们。然后我希望他们等到方法执行完成后再继续。我的示例解决方案如下所示。

    public static void Main()
{
Console.WriteLine("Main thread starting.");
String[] strThreads = new String[] { "one", "two" };

String ctemp = string.Empty;
foreach (String c in strThreads)
{
ctemp = c;
Thread thread = new Thread(delegate() { MethodCall(ctemp); });
thread.Start();
thread.Join();
}

Console.WriteLine("Main thread ending.");
Console.Read();
}


public static void MethodCalls(string number)
{
Console.WriteLine("Method call " + number);
}

这会完成这项工作吗?还是有另一种更好的方法来做同样的事情?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com