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

  • SEARCH
  • Home
  • 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 8985317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:18:14+00:00 2026-06-15T21:18:14+00:00

I have a for loop running through 500.000ish list. For each of these it

  • 0

I have a for loop running through 500.000ish list. For each of these it is queueing up a SmartThreadPool job.

lines.Length below contains 500.000ish items.

My problem is that i get memory issues when queueing them all at once.. So i though id write a logic to prevent this:

int activeThreads = _smartThreadPool2.ActiveThreads;
if (activeThreads < maxThreads) 
{
    int iia = 0;
    for (int i = 0; i < lines.Length; i++)
    {
        if (doNotUseAdditive.Checked == true)
        {
            foreach (string engine in _checkedEngines) // Grab selected engines
            {
                query = lines[i];

                _smartThreadPool2.QueueWorkItem(
                new Amib.Threading.Func<string, string, int, int, int>(scrape),
                query, engine, iia, useProxies);

                iia++;
            }
        }
    }
}
else
{
    // Wait
    wait.WaitOne();
}

The problem is that i cannot run that if statement inside my for loop, because when i come back to it, it will not remember where it was inside the loop.

I’m using a:

ManualResetEvent wait = new ManualResetEvent(false);  //global variable

To “Pause/Resume”

I need to somehow pause the loop after X threads are used and then when threads are available return and continue the loop.

Any ideas?

  • 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-15T21:18:15+00:00Added an answer on June 15, 2026 at 9:18 pm

    I don’t think that process every item in list in separate thread is a good idea. Even using custom thread pool can be really error-prone (and you examples proves my opinion).

    First of all you should determine number of working threads correctly. It seems that you’re dealing with computation intensive operations (so called CPU Bound operations) and you should use number of working threads equals to number of logical processors.

    Than you can use Parallel LINQ to split all your working set for appropriate amount of chunks and process those chunks in parallel.

    Joe Albahari has a great series of posts about this topic: Threading in C#. Part 5. Parallel Programming.

    Here is a pseudocode of using PLINQ:

    lines
      .AsParallel()
      .WithDegreeOfParallelism(YourNumberOfProcessors)
      .Select(e => ProcessYourData(e));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background Info: I have a program running through a list of url; each URL
I have this simple loop, running through an array of base class pointers: Object
I have a function that displays a few divs running through a loop using
I have a loop that goes through a huge string of characters. Checks each
I'm running through a loop in which I have this code: if (i %
I have a Java program performing some large analysis tasks, running through a loop
I have a Parallel.ForEach loop running an intensive operation inside the body. The operation
I have a program in Octave that has a loop - running a function
I have a PHP script with infinite loop. I need this script running forever.
Suppose you have the following function foo . When I'm running a for loop,

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.