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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:05:17+00:00 2026-05-23T00:05:17+00:00

I’m currently working on a project, where we have the challenge to process items

  • 0

I’m currently working on a project, where we have the challenge to process items in parallel. So far not a big deal 😉
Now to the problem. We have a list of IDs, where we periodically (every 2 sec’s) what to call a StoredProcedure for each ID.
The 2 sec’s need to be checked for each item individually, as they are added and removing during runtime.
In addition we want to configure the maximum degree of parallelism, as the DB should not be flooded with 300 threads concurrently.
An item which is being processed should not be rescheduled for processing until it has finished with the previous execution. Reason is that we want to prevent queueing up a lot of items, in case of delays on the DB.

Right now we are using a self-developed component, that has a main thread, which periodically checks what items need to scheduled for processing. Once it has the list, it’s dropping those on a custom IOCP-based thread pool, and then uses waithandles to wait for the items being processed. Then the next iteration starts. IOCP because of the work-stealing it provides.

I would like to replace this custom implementation with a TPL/.NET 4 version, and I would like to know how you would solve it (ideally simple and nicely readable/maintainable).
I know about this article: http://msdn.microsoft.com/en-us/library/ee789351.aspx, but it’s just limiting the amount of threads being used. Leaves work stealing, periodically executing the items ….

Ideally it will become a generic component, that can be used for some all the tasks that need to be done periodically for a list of items.

any input welcome,
tia
Martin

  • 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-23T00:05:18+00:00Added an answer on May 23, 2026 at 12:05 am

    I don’t think you actually need to get down and dirty with direct TPL Tasks for this. For starters I would set up a BlockingCollection around a ConcurrentQueue (the default) with no BoundedCapacity set on the BlockingCollection to store the IDs that need to be processed.

    // Setup the blocking collection somewhere when your process starts up (OnStart for a Windows service)
    BlockingCollection<string> idsToProcess = new BlockingCollection<string>();
    

    From there I would just use Parallel::ForEach on the enumeration returned from the BlockingCollection::GetConsumingEnumerable. In the ForEach call you will setup your ParallelOptions::MaxDegreeOfParallelism Inside the body of the ForEach you will execute your stored procedure.

    Now, once the stored procedure execution completes, you’re saying you don’t want to re-schedule the execution for at least two seconds. No problem, schedule a System.Threading.Timer with a callback which will simply add the ID back to the BlockingCollection in the supplied callback.

    Parallel.ForEach(
        idsToProcess.GetConsumingEnumerable(),
        new ParallelOptions 
        { 
            MaxDegreeOfParallelism = 4 // read this from config
        },
        (id) =>
        {
           // ... execute sproc ...
    
           // Need to declare/assign this before the delegate so that we can dispose of it inside 
           Timer timer = null;
    
           timer = new Timer(
               _ =>
               {
                   // Add the id back to the collection so it will be processed again
                   idsToProcess.Add(id);
    
                   // Cleanup the timer
                   timer.Dispose();
               },
               null, // no state, id wee need is "captured" in the anonymous delegate
               2000, // probably should read this from config
               Timeout.Infinite);
        }
    

    Finally, when the process is shutting down you would call BlockingCollection::CompleteAdding so that the enumerable being processed with stop blocking and complete and the Parallel::ForEach will exit. If this were a Windows service for example you would do this in OnStop.

    // When ready to shutdown you just signal you're done adding
    idsToProcess.CompleteAdding();
    

    Update

    You raised a valid concern in your comment that you might be processing a large amount of IDs at any given point and fear that there would be too much overhead in a timer per ID. I would absolutely agree with that. So in the case that you are dealing with a large list of IDs concurrently, I would change from using a timer-per-ID to using another queue to hold the “sleeping” IDs which is monitored by a single short interval timer instead. First you’ll need a ConcurrentQueue onto which to place the IDs that are asleep:

    ConcurrentQueue<Tuple<string, DateTime>> sleepingIds = new ConcurrentQueue<Tuple<string, DateTime>>();
    

    Now, I’m using a two-part Tuple here for illustration purposes, but you may want to create a more strongly typed struct for it (or at least alias it with a using statement) for better readability. The tuple has the id and a DateTime which represents when it was put on the queue.

    Now you’ll also want to setup the timer that will monitor this queue:

    Timer wakeSleepingIdsTimer = new Timer(
       _ =>
       {
           DateTime utcNow = DateTime.UtcNow;
    
           // Pull all items from the sleeping queue that have been there for at least 2 seconds
           foreach(string id in sleepingIds.TakeWhile(entry => (utcNow - entry.Item2).TotalSeconds >= 2))
           {
               // Add this id back to the processing queue
               idsToProcess.Enqueue(id);
           }
       },
       null, // no state
       Timeout.Infinite, // no due time
       100 // wake up every 100ms, probably should read this from config
     );
    

    Then you would simply change the Parallel::ForEach to do the following instead of setting up a timer for each one:

    (id) =>
    {
           // ... execute sproc ...
    
           sleepingIds.Enqueue(Tuple.Create(id, DateTime.UtcNow)); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.