开发者

"There were not enough free threads in the ThreadPool to complete the operation."

I got the following error message while downloading using multiple threads:

There were not e开发者_如何学Gonough free threads in the ThreadPool to complete the operation.

Should I try to change the default settings for the threadpool in the .net, and how can I do it?


Changing the size of the ThreadPool is almost certainly a bad idea (not least: each thread will take another chunk of stack space); you should look instead at your thread usage - are you saturating the pool? You might find TPL and Task[<T>] a better way to handle some async code, or in the cases of things like IO - the Begin* / End* async operations rather than a sync operation on a ThreadPool thread.

You might also check that all your async operations are existing cleanly; are they perhaps stalling (deadlock perhaps) and not exiting?

Finally: the ThreadPool is primarily intended for brief operations (where the spin-up cost of a Thread is most noticeable); long-running operations may do better on dedicated Threads


Although this doesn't answer the question directly, it may help someone to avoid this problem. You can use the below to see how many threads are available and make sure you do not exceed it.

int workerThreads, cmpThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out cmpThreads);

This will give you the max threads available.

For a description for the different types of threads, have a look at this explanation:

Simple description of worker and I/O threads in .NET


I'm not sure if this would be the best idea in your situation but if you decide you really need to expand the thread pool this is one way to do it.

 public static void ExpandThreadPool(int minThreads,int maxThreads)
    {
        int workerThreads, cmpThreads;

        ThreadPool.GetMaxThreads(out workerThreads, out cmpThreads);
        if (workerThreads < maxThreads)
        {
            workerThreads = maxThreads;
        }
        ThreadPool.SetMaxThreads(workerThreads, cmpThreads);

        ThreadPool.GetMinThreads(out workerThreads, out cmpThreads);
        if (workerThreads < minThreads)
        {
            workerThreads = minThreads;
        }
        ThreadPool.SetMinThreads(workerThreads, cmpThreads);

    }


You can emulate the program as AspNetServer:

if (Thread.GetDomain().GetData(".appDomain") == null)
{
    Thread.GetDomain().SetData(".appDomain", new object());
}

See this method System.Net.NclUtilities.IsThreadPoolLow:

if (ComNetOS.IsAspNetServer)
{
    return false;
}

And this System.Net.ComNetOS .ctor():

IsAspNetServer = Thread.GetDomain().GetData(".appDomain") != null;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜