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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:32:50+00:00 2026-05-28T13:32:50+00:00

I have an object in my application which performs processing on the items in

  • 0

I have an object in my application which performs processing on the items in a collection in a background thread. When the object is created background processing of all existing items in the collection is triggered using the thread pool:

class CollectionProcessor
{
    public CollectionProcessor()
    {
        // Not actually called during the constructor just put it here to simplify the code sample
        Action process = new Action(this.Process);
        createIndex.BeginInvoke(true, ar => process.EndInvoke(ar), null);
    }

    void Process()
    {
        for (int i = 0; i < this.items.Count; i++)
        {
            this.ProcessItem(this.items[i]);
        }
    }
}

There is some extra code dotted around for notification callbacks but that is largely the gist of it.

New items can be added to this collection at any time and I need to make sure that those new items are processed – notification of new items is provided by an event which is fired after the items have already been added to the collection. In the event hanlder for this event I need to asynchronously resume the processing of the new items in the collection while also:

  • Ensuring that I don’t process the same item twice
  • Ensuring that the items are processed in the correct order
  • Avoiding queuing up lots of blocked background tasks

I also want to achieve this using a thread pool instead of using a dedicated thread – How should I do this? Obviously assume that access to this.items is thread-safe.

  • 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-28T13:32:50+00:00Added an answer on May 28, 2026 at 1:32 pm

    I believe I have figured out a reasonably neat way of doing this. They key is to note that if I had a dedicated background thread performing this processing then the solution is fairly easy and might look a little like this:

    AutoResetEvent ev = new AutoResetEvent(false);
    
    // Called on a background thread
    void ThreadProc()
    {
        int lastProcessed = 0;
        while (true)
        {
            // Perform our processing as before
            for (int i = lastProcessed; i < this.items.Count; i++)
            {
                this.ProcessItem(this.items[i]);
            }
    
            // We have processed all items currently in the list, wait for some more
            ev.WaitOne();
        }
    }
    
    void OnNewItems()
    {
        ev.Set();
    }
    

    The missing link is the ThreadPool.RegisterWaitForSingleObject Method which allows us to convert this to using a thread pool instead of a dedicated thread:

    int lastProcessed = 0;
    
    void StartProcessing()
    {
        ThreadPool.RegisterWaitForSingleObject(
            this.ev,
            new WaitOrTimerCallback(WaitProc),
            null,   // All state stored in the class instance itself
            -1,     // Always wait indefinitely for new items
            true    // Only execute once - each callback registers a new wait handle ensuring
                    // that a maximum of 1 task is running Process at any one time
        );
    }
    
    void WaitProc(object state, bool timedOut)
    {
        // Perform our processing as before
        for (int i = lastProcessed; i < this.items.Count; i++)
        {
            this.ProcessItem(this.items[i]);
        }
    
        // We have processed all items currently in the list, wait for some more
        this.StartProcessing();
    }
    

    This sets up a loop just as before except we aren’t blocking a thread waiting for the reset event.

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

Sidebar

Related Questions

I have created a Class Library (called as GNGEngine.dll) which performs some image processing
I have a .NET application which serializes an object in binary format. this object
I have a PHP web application which uses a MySQL database for object tagging,
I have a spring-hibernate application which is failing to map an object properly: basically
I have my main application delegate which contains a method that returns an object.
I'm developing an application which currently have hundreds of objects created. Is it possible
In my application, I have three collection objects which store data. The data which
I have an object created in a host application and can access it remotely
I have a Generic HTTP Handler (*.ashx) in my ASP.Net application which performs some
I have a multithreaded application in which my thread utilization is very poor (in

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.