0

I'm newbie to use threading logic.

I declared ThreadPool.SetMaxThreads(10, 10) and create multiple thread same number of 10.

First request working well, I requested 2 more each different browser. following request fallen hang until finish first request's thread work.

Does ThreadPool.SetMaxThreads affected entire IIS application pool?

public ActionResult Index()
{
    ThreadPool.SetMaxThreads(10, 10);

    for (int i = 0; i < 10; i++)
    {
        Task.Factory.StartNew(() =>
        {

            try
            {
                Thread.Sleep(30000);
            }
            finally
            {

            }
        });

    }       

    return View();

}

Here's new code using semaphoreslim. My Actual goal is only specified count of threads run at once. for example I will download 9 files and assign each thread 1 download job, max 3 threads work.

public ActionResult Index()
    {
        int maxConcurrency = 3;

        using (SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(maxConcurrency))
        {
            List<Task> tasks = new List<Task>();

            for (int i = 0; i < 9; i++)
            {
                concurrencySemaphore.Wait();

                var t = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        // Here to actual my job logic.
                        Thread.Sleep(10000);
                    }
                    finally
                    {
                        concurrencySemaphore.Release();
                    }
                });

                tasks.Add(t);
            }

            Task.WaitAll(tasks.ToArray());
        }

        return View();
    }
shunman
  • 227
  • 1
  • 2
  • 9
  • This feels like a XY Problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . What do you think `SetMaxThreads` does? Why are you calling it? What problem are you trying to solve with that call? – mjwills Nov 08 '18 at 04:31
  • _You **definitely** shouldn't be calling it inside an action - at app startup it can make sense, but it virtually never makes sense in the specific context of an individual web request._ Have you read the docs? https://learn.microsoft.com/en-au/dotnet/api/system.threading.threadpool.setmaxthreads?view=netframework-4.7.2 – mjwills Nov 08 '18 at 04:32
  • Possible duplicate of [What does MaxDegreeOfParallelism do?](https://stackoverflow.com/questions/9538452/what-does-maxdegreeofparallelism-do) – mjwills Nov 08 '18 at 04:34
  • No @shunman - it applies it to the entire AppDomain / process (I can't remember which - but either way it isn't what you want). Check the last link I provided. You want that instead. – mjwills Nov 08 '18 at 04:42
  • @mjwills Yes. That article describe about SetMaxThreads."All requests above that number remain queued until thread pool threads become available." means that include web request? – shunman Nov 08 '18 at 04:50
  • Try and explain why you think you need a semaphore. What is your **real problem**? – mjwills Nov 08 '18 at 05:03
  • Note about `semephoreSlim`, its based on a SpinLock, and is not ideal to wait for extended periods of time. In short, designing a limited-concurrency solution out of `SemaphoreSlim` will use precious CPU-cycles that in-turn could be given to kids in other countries who don't have them – TheGeneral Nov 08 '18 at 05:17
  • @mjwills Thank you for patience. I updated my new code and goal. And I just want to get about SetMaxThreads affect by entire iis thread environment – shunman Nov 08 '18 at 05:17
  • You want to use https://stackoverflow.com/questions/9538452/what-does-maxdegreeofparallelism-do . – mjwills Nov 08 '18 at 05:30
  • @mjwills you already give a answer. "Do not use ThreadPool.SetMaxThreads " I'll read it thanks again – shunman Nov 08 '18 at 05:40
  • 1
    The code you posted is very suspect, and most likely not the right way to do what you want to do (what ever that is) just be warned – TheGeneral Nov 08 '18 at 05:43

0 Answers0