简体   繁体   English

等待线程池的线程完成

[英]Wait for threadpool's threads to finish

I've got this method: 我有这种方法:

/// <summary>
    /// Waits for all threads in the thread pool to be finished.         
    /// </summary>
    /// <param name="maxWaitingTime">Maximum time to wait in seconds</param>
    /// <returns>true if all threads in pool are finished before maxWaitingTime. False if maxWaitingTime is hit </returns>
    public bool WaitForThreads(int maxWaitingTime)
    {
        int maxThreads = 0;
        int placeHolder = 0;
        int availableThreads = 0;

        while (maxWaitingTime > 0)
        {
            System.Threading.ThreadPool.GetMaxThreads(out maxThreads, out placeHolder);
            System.Threading.ThreadPool.GetAvailableThreads(out availableThreads, out placeHolder);

            //Stop if all threads are available
            if (availableThreads == maxThreads)
            {
                return true;
            }

            System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(1000));
            --maxWaitingTime;            
        }

        return false;
    }

I'm using this in a console application to wait for a while so all tasks that threadpool is handling are finished. 我在控制台应用程序中使用它来等待一段时间,以便线程池正在处理的所有任务都已完成。 I've set the max threads to 8, but somehow the available threads never reach the max threads, there is always one thread still busy. 我将最大线程数设置为8,但是以某种方式可用线程永远不会达到最大线程数,总是有一个线程仍在忙。

I'm thinking that somehow this method is called within the thread pool, even if I call this method directly from the program.cs. 我在想,即使直接从program.cs调用此方法,也要在线程池中调用此方法。

How can I see in the threads window of visual studio if a thread is run in the thread pool? 我如何在Visual Studio的线程窗口中查看线程池中是否运行了线程?

That's not what "maxThreads" means. 那不是“ maxThreads”的意思。 The job of the threadpool scheduler is to keep the number of active threads equal to "minThreads". 线程池调度程序的工作是保持活动线程数等于“ minThreads”。 Only when those threads don't complete in a reasonable amount of time (reasonable is half a second), then the scheduler makes another thread available to try to work down the backlog. 只有当这些线程没有在合理的时间内完成(合理的是半秒)时,调度程序才会使另一个线程可用以尝试减少积压。 The "maxThreads" value sets an upper bound on how many additional threads it makes available. “ maxThreads”值设置了它提供多少个附加线程的上限。

So what you really want to test is to check if the number of available threads is equal or larger to "minThreads". 因此,您真正要测试的是检查可用线程数是否等于或大于“ minThreads”。

This is a very bad practice, decent odds that your code will just deadlock. 这是一个非常糟糕的做法, 可能您的代码将陷入僵局。 Which will happen when the code gets accidentally in sync with a Timer or TP threads that you just don't know about because they got started by .NET Framework code. 当代码意外地与您不知道的Timer或TP线程同步时,会发生这种情况,因为它们是由.NET Framework代码启动的。 Waiting for TP threads to complete reliably requires writing explicit code to check for the condition, those threads must signal a synchronization object when they exit so you can use WaitHandle.WaitAll(). 等待TP线程可靠地完成需要编写显式代码以检查条件,这些线程在退出时必须向同步对象发出信号,以便可以使用WaitHandle.WaitAll()。 Best done with the Task.WaitAll() method. 最好使用Task.WaitAll()方法完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM