"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 enough 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?

Answers


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;

Need Your Help

iOS: Find language of current location

ios swift

I am making an app where I want to detect the language of the current country that the iPhone is in. By this I don't mean the locale (which doesn't change if you're vacationing in a foreign country...

save() not working in ext4yii ExtFormController

extjs yii

i tried to insert data using ext4yii form. But the save function in not working. plz look through my code