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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:07:32+00:00 2026-06-09T07:07:32+00:00

I’m writing a polling system against SQL Server. When tasks are retrieved, they are

  • 0

I’m writing a polling system against SQL Server. When tasks are retrieved, they are off-loaded to a new thread using Task.Factory.StartNew(). The whole thing is inside an endless loop and I want to allow only N concurrent tasks.

while(true)
{
    // create connection

    while(_concurrentTasks < _allowedTasks)
    { 
        // get job info

        if(!reader.HasRows) break; // no work to do.

        while(reader.Read())
        {
            // fill JobInfo entity from reader

            _taskCollection.Add(Task.Factory.StartNew(() => ProcessJob(jobInfo)));

            _concurrentTasks++;
        }
    }

    // could break for 1 of 2 reasons, threshold met or nothing to do
    if((_concurrentTasks < _allowedTasks)
    {
        // sleep for a predetermined period of time
        Thread.Sleep(60000);
    }
    else
    {
        // wait for one thread to complete and then get more jobs
        Task.WaitAny(_taskCollection.ToArray);   
    }
}

I’m not sure which collection would be best to use in this situation or how to clean up the tasks that have been completed.

If I put the cleanup code in the task itself, I can use List<Task> along with the Task.CurrentId property to identify the item in the collection, but I don’t believe I can dispose of the Task object in the collection within the task itself; also note that the collection would have to be threadsafe.

If I put some kind of cleanup code outside the task (in the main thread), I wouldn’t need a threadsafe collection, but I have no idea when a task completes in the main thread.

So, which collection should I use to maintain a list/array of concurrent tasks so I can use WaitAny() and how can I clean up the list when one completes?

  • 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-09T07:07:34+00:00Added an answer on June 9, 2026 at 7:07 am

    I’ve decided to use an array and loop through it when necessary:

    Task[] tasks = new Task[_allowedTasks];
    
    while(true)  
    {  
        // create connection  
    
        while(_concurrentTasks < _allowedTasks)  
        {   
            // get job info  
    
            if(!reader.HasRows) break; // no work to do.  
    
            while(reader.Read())  
            {  
    
                for (int i = 0; i < tasks.Length; i++)
                {
                    if (tasks[i] == null || tasks[i].IsCompleted)
                    {
                        // Dispose of Task at position i
    
                        nextSlot = i;
                        break;
                    }
                }
    
                // fill JobInfo entity from reader  
    
                tasks[nextSlot] = Task.Factory.StartNew(() => ProcessJob(jobInfo));  
    
                _concurrentTasks++;  
            }  
        }  
    
        // could break for 1 of 2 reasons, threshold met or nothing to do  
        if((_concurrentTasks < _allowedTasks)  
        {  
            // sleep for a predetermined period of time  
            Thread.Sleep(60000);  
        }  
        else  
        {  
            // wait for one thread to complete and then get more jobs  
            Task.WaitAny(_taskCollection.ToArray);     
        }  
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.