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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:00:30+00:00 2026-05-24T23:00:30+00:00

I have a loop that looks something like this: var list = new List<float>();

  • 0

I have a loop that looks something like this:

var list = new List<float>();
while (list.Count < wantedNumberOfJobs)
{
    var resource = ... //gets the resource
    lock (resource)
    {    
        ThreadPool.QueueUserWorkItem(DoWork, /*wrap resource and list into an object*/);
    }
}

//continue pass this point when all the threads are finished

And the work method:

private void DoWork(object state)
{
    var list = (/*wrapperObject*/)state.List;
    var someFloat = //do the work to get the number

    lock (list)
    {
        list.Add(someFloat);
    }
}

Essentially I want to do a large, but a specific (given by wantedNumberOfJobs) number of jobs done. Each of these jobs inserts a single item into the list as you can see in the DoWork method.

I am not certain that this code is assuring me that list will contain wantedNumberOfJobs items past the designated point. I’d also like to limit the number of active threads. I’ve used the System.Threading.Semaphore class, but am not sure this is the best solution.

I’d appreciate any help. Thanks!

  • 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-24T23:00:30+00:00Added an answer on May 24, 2026 at 11:00 pm

    Perhaps you can use Parallel.For, like so:

    Parallel.For(0, wantedNumberOfJobs, i => {
                    var resource = ... //gets the resource   
                    DoWork(Resource);
                });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple server that looks something like this: void *run_thread(void *arg) {
I have a loop that looks like this def slow_loop(array) array.each_with_index do |item, i|
I have a text file that looks something like this where the first column
I have a loop that looks like this: for (int i = 0; i
I have a code that looks something like this: struct First { int f1;
I'm working on a graphical application which looks something like this: while (Simulator.simulating) {
I have a file that looks like this: A B C D E 0
I have a class that looks like this: public class MyModel{ public int TheId
So i Have a list which is something like this: list=[10.0, 10.0, 10.0, 9.9,
I have a for loop that loops over one array... for i=1:length(myArray) In this

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.