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 7770047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:18:44+00:00 2026-06-01T16:18:44+00:00

I have a simply foreach loop that limits itself based on while loops and

  • 0

I have a simply foreach loop that limits itself based on while loops and a static int. If I dont limit it, my CPU stays under 10% if i limit it my CPU goes up to 99/100%. How do I safely limit the number of calls to a class within a Paralell.Foreach?

static int ActiveThreads { get; set; }
static int TotalThreads { get; set; }

var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 1;
Parallel.ForEach(urlTable.AsEnumerable(),options,drow =>
{
    using (var WCC = new MasterCrawlerClass())
    {
        while (TotalThreads <= urlTable.Rows.Count)
        {
            if (ActiveThreads <= 9)
            {
                Console.WriteLine("Active Thread #: " + ActiveThreads);
                ActiveThreads++;
                WCC.MasterCrawlBegin(drow);
                TotalThreads++;
                Console.WriteLine("Done Crawling a datarow");
                ActiveThreads--;
            }
        }
    }
});

I need to limit it, and yes I understand Max Parallelism has it’s own limit, however, my switch gets bogged down before the CPU in the server hits that limit.

  • 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-06-01T16:18:45+00:00Added an answer on June 1, 2026 at 4:18 pm

    Two things :

    1) You don’t seem to be using your ParallelOptions() that you created in this example.

    2) You can use a Semaphore if for some reason you don’t want to use the ParallelOptions.

     Semaphore sm = new Semaphore(0, 9);
    
     // increment semaphore or block if = 9
     // this will block gracefully without constantly checking for `ActiveThreads <= 9`
     sm.WaitOne();
    
     // decrement semaphore
     sm.Release();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Parallel.Foreach loop that has about 1000 rows in the DataTable,
hey guys, we have a loop that: 1.Loops over several thousand xml files. Altogether
I have a simple foreach loop that goes through the productID's I have stored
in my asp.net mark up I have a foreach loop that iterates through a
Yay I finally thought of a title! I have a foreach loop that iterates
I have a very simply subclass of UIButton that will fire off a custom
May have been asked before, but not that I could find. I'm simply wondering
I have a pretty simple case which I started solving using foreach(), but then
I have a very simple query $this->db->select('id, title'); $this->db->order_by('id', 'DESC'); $query = $this->db->get('journal'); foreach($query->result()
I simply have 2 tabs and used Experience - Multiple Android Activities in a

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.