C#如何判断线程池中所有的线程是否已经完成之Demo
2018-02-06 09:58 狼人:-) 阅读(562) 评论(0) 编辑 收藏 举报start: System.Threading.RegisteredWaitHandle rhw = null; new Action(() => { for (var i = 0; i < 30; i++) { new Action<int>((index) => { System.Threading.Thread.Sleep(1000); Console.WriteLine(System.Threading.Thread.CurrentThread.Name + "执行完毕" + index); }).BeginInvoke(i, null, null); } }).BeginInvoke(null, null); rhw = System.Threading.ThreadPool.RegisterWaitForSingleObject(new System.Threading.AutoResetEvent(false), new System.Threading.WaitOrTimerCallback((obj, b) => { int workerThreads = 0; int maxWordThreads = 0; //int int compleThreads = 0; System.Threading.ThreadPool.GetAvailableThreads(out workerThreads, out compleThreads); System.Threading.ThreadPool.GetMaxThreads(out maxWordThreads, out compleThreads); //Console.WriteLine(workerThreads); //Console.WriteLine(maxWordThreads); //当可用的线数与池程池最大的线程相等时表示线程池中所有的线程已经完成 if (workerThreads == maxWordThreads) { //当执行此方法后CheckThreadPool将不再执行 rhw.Unregister(null); //此处加入所有线程完成后的处理代码 Console.WriteLine("f"); rhw = null; } }), null, 100, false); while (rhw != null) { } System.Threading.Thread.Sleep(10000) ; goto start; Console.WriteLine("finished");
声明:此博有部分内容为转载,版权归原作者所有~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2017-02-06 邮件收取客户端LumiSoft类库接收yahoo邮件的问题。
2012-02-06 亲爱的老板:程序员的10分钟就是3个小时