Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:02:25+00:00 2026-05-30T20:02:25+00:00

I am using Parallel.ForEach and I am doing some database updates, now without setting

  • 0

I am using Parallel.ForEach and I am doing some database updates, now without setting MaxDegreeOfParallelism, a dual core processor machine results in SQL client timeouts, where else quad core processor machine somehow does not timeout.

Now I have no control over what kind of processor cores are available where my code runs, but is there some settings I can change with MaxDegreeOfParallelism that will probably run less operations simultaneously and not result in timeouts?

I can increase timeouts but it isn’t a good solution, if on lower CPU I can process less operations simultaneously, that will put less load on cpu.

Ok I have read all other posts and MSDN too, but will setting MaxDegreeOfParallelism to lower value make my quad core machines suffer?

For example, is there anyway to do something like, if CPU has two cores, then use 20, if CPU has four cores then 40?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:02:26+00:00Added an answer on May 30, 2026 at 8:02 pm

    The answer is that it is the upper limit for the entire parallel operation, irrespective of the number of cores.

    So even if you don’t use the CPU because you are waiting on IO, or a lock, no extra tasks will run in parallel, only the maximum that you specifiy.

    To find this out, I wrote this piece of test code. There is an artificial lock in there to stimulate the TPL to use more threads. The same will happen when your code is waiting for IO or database.

    class Program
    {
        static void Main(string[] args)
        {
            var locker = new Object();
            int count = 0;
            Parallel.For
                (0
                 , 1000
                 , new ParallelOptions { MaxDegreeOfParallelism = 2 }
                 , (i) =>
                       {
                           Interlocked.Increment(ref count);
                           lock (locker)
                           {
                               Console.WriteLine("Number of active threads:" + count);
                               Thread.Sleep(10);
                            }
                            Interlocked.Decrement(ref count);
                        }
                );
        }
    }
    

    If I don’t specify MaxDegreeOfParallelism, the console logging shows that up to around 8 tasks are running at the same time. Like this:

    Number of active threads:6
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:6
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    Number of active threads:7
    

    It starts lower, increases over time and at the end it is trying to run 8 at the same time.

    If I limit it to some arbitrary value (say 2), I get

    Number of active threads:2
    Number of active threads:1
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    Number of active threads:2
    

    Oh, and this is on a quadcore machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question centers on some Parallel.ForEach code that used to work without fail, and
I have some experience using parallel extensions in .Net development, but I was looking
I'm using TPL in my current project and using Parallel.Foreach to spin many threads.
I'm using C# with .net 4.0 Is ThreadPool.QueueUserWorkItem thread safe? Example: Parallel.ForEach(commands, commandModel =>
I am working on a application which downloads using 1) Parallel.ForEach(linklist, x => DownloadAsync(x));
So I'm working in c# 4.0 WPF application and using parallel foreach loops to
How would I register a PSOCK cluster (created using package Parallel) with foreach ?
Is there a way to guarantee order when using Parallel.ForEach() ? The collection I
I'm looking into using parallel unit tests for our project(s) and was wondering about
I'm using parallel-python and start a new job server in a function. after the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.