zoukankan      html  css  js  c++  java
  • C#如何判断线程池中所有的线程是否已经完成之Demo

    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");
  • 相关阅读:
    嵌入级联分类器
    AdaBoost 和 Real Adaboost 总结
    二分图匹配--匈牙利算法
    更新说明
    使用Visual Studio 2015 Community 开发windows服务
    C#字符串的不变性
    Windows 7 IIS HTTP 错误 500.21 – Internal Server Error 解决方法
    asp.net的请求管道事件
    Http请求过程
    css简单学习属性2---背景图片
  • 原文地址:https://www.cnblogs.com/waw/p/8420933.html
Copyright © 2011-2022 走看看