C# 多线程等待子线程全部完成 ThreadPool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | private void button1_Click( object sender, EventArgs e) { //假设有10个请求线程 int num = 10; using (MultiThreadResetEvent multiThreadResetEvent = new MultiThreadResetEvent(num)) { for ( int i = 0; i < num; i++) { //ManualResetEvent manualResetEvent = new ManualResetEvent(false); Param pra = new Param(); pra.multiThreadResetEvent = multiThreadResetEvent; pra.value = i; //manualResetEvents.Add(manualResetEvent); ThreadPool.QueueUserWorkItem(Call, pra); } //WaitHandle.WaitAll(manualResetEvents.ToArray()); multiThreadResetEvent.WaitAll(); } Console.WriteLine( "main thread is success" ); } |
1 2 3 4 5 6 7 8 9 | private void Call( object state) { Thread.Sleep(1000); Param param = state as Param; Console.WriteLine($ "Thread execute {param.value} --- {DateTime.Now} " ); param.multiThreadResetEvent.SetOne(); } |
1 2 3 4 5 6 7 8 9 | public class Param { public Param() { } public MultiThreadResetEvent multiThreadResetEvent { get ; set ; } public int value { get ; set ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | /// <summary> /// 多线程信号等待 /// </summary> public class MultiThreadResetEvent : IDisposable { private readonly ManualResetEvent done; private readonly int total; private long current; /// <summary> /// 监视total个线程执行(线程数固定,可以超过64个) /// </summary> /// <param name="total">需要等待执行的线程总数</param> public MultiThreadResetEvent( int total) { this .total = total; current = total; done = new ManualResetEvent( false ); } /// <summary> /// 线程数不固定,监视任意线程数时 /// </summary> public MultiThreadResetEvent() { done = new ManualResetEvent( false ); } /// <summary> /// 加入一个要等待的线程信号 /// </summary> public void addWaitOne() { Interlocked.Increment( ref current); } /// <summary> /// 唤醒一个等待的线程 /// </summary> public void SetOne() { // Interlocked 原子操作类 ,此处将计数器减1 if (Interlocked.Decrement( ref current) == 0) { //当所以等待线程执行完毕时,唤醒等待的线程 done.Set(); } } /// <summary> /// 等待所以线程执行完毕 /// </summary> public void WaitAll() { done.WaitOne(); } /// <summary> /// 释放对象占用的空间 /// </summary> public void Dispose() { ((IDisposable)done).Dispose(); } } |
测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」