C#多线程问题,如何知道所有的线程都执行完毕
展开全部
这个需求有很多方法可以实现,给你一个最简单的方法的例子:
using System;using System.Threading;public class ThreadDemo{ static void Main() { Console.WriteLine("Hello, I'm going to do some time-consuming work..."); for (int i = 0; i < 5; i++) { Thread t = new Thread(Work); t.Start(); t.Join(); } Console.WriteLine("I'm finished all works on my hand. You can do something else."); // Some other works. } static void Work() { // Simulate time-consuming work Thread.Sleep(1000); Console.WriteLine("Working..."); }}
using System;using System.Threading;public class ThreadDemo{ static void Main() { Console.WriteLine("Hello, I'm going to do some time-consuming work..."); for (int i = 0; i < 5; i++) { Thread t = new Thread(Work); t.Start(); t.Join(); } Console.WriteLine("I'm finished all works on my hand. You can do something else."); // Some other works. } static void Work() { // Simulate time-consuming work Thread.Sleep(1000); Console.WriteLine("Working..."); }}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询