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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:06:30+00:00 2026-05-18T10:06:30+00:00

I have a simple multi-threaded program, with the following structure. public void someFunction() {

  • 0

I have a simple multi-threaded program, with the following structure.

public void someFunction()
{

    List<object> objList = new List<object>();
    //objList populated here

    foreach(object o in objList)
    {
        Thread aThread = new Thread (new ParameterizedThreadStart(doSomething));
        aThread.Start(o);
    }
}

static void doSomething(object o)
{
    //Do something with o.
}

This works well, but I’m having issues limiting the number of simultaneous theads running. Say I want to define int maxThreads = 25 for example. The best way I could think of doing it would be something like:

SomeThreadsafeCounter c = new SomeThreadsafeCounter();

foreach(object o in objList)
{
    while (c < 26){ wait; }
    c++;
    Thread aThread = new Thread (new ParameterizedThreadStart(doSomething));
    aThread.Start(o);
    c--;
}

I’m certain this isn’t the correct way to go about it, is there a better way?

  • 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-18T10:06:30+00:00Added an answer on May 18, 2026 at 10:06 am

    You can do this with the Task parallel library included with .net 4.

    ParallelOptions options = new ParallelOptions();
    options.MaxDegreeOfParallelism = 25;
    
    Parallel.ForEach(objList, options, item => doSomething(item));
    

    Parallel.ForEach has a number of overloads and I am not sure that I have the correct one here.

    More info here: http://msdn.microsoft.com/en-us/library/dd537608.aspx

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

Sidebar

Related Questions

I have a simple C# Winforms application. It isn't multi-threaded (yet). When I debug
I have a simple multi-threaded .NET app which inserts rows into an MS Access
I'm using Thread::Pool::Simple for multi-threading. I have a couple of questions which are quite
Sometimes, when I have a multi-case if , or a very simple for ,
I have found this example on StackOverflow: var people = new List<Person> { new
In my multi-threaded project(C# 3.5), I have many codes simliar to this: Map map;
I have a multi-threaded script which occasionally freezes when it connects to a server
I have never written multi-threaded code before (barring a few basic backgroundworker tricks) and
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using

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.