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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:35:28+00:00 2026-06-14T15:35:28+00:00

I have a list of data sets that need to be updated real time.

  • 0

I have a list of data sets that need to be updated real time. I would like to process 10 out of 100+ at a time and then once one is done grab the next oldest line. Basically keep this loop going for an infinite amount of time. I am quite new to the world of Threading and been poking around at AsyncTask. Is there an example of this anyone can point me to? I have googled quite a bit but can’t find exactly what i’m looking for.

  • 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-14T15:35:29+00:00Added an answer on June 14, 2026 at 3:35 pm

    AsyncTask is better suited to one-off operations. For an ongoing task you can consider a worker thread.

    Disclaimer: I don’t claim this is the best way to do it, but it should give you some ideas and stuff to read up on.

        public class ThreadingSample : IDisposable
    {
        private Queue<SomeObject> _processingQueue = new Queue<SomeObject>();
        private Thread _worker;
        private volatile bool _workerTerminateSignal = false;
        private EventWaitHandle _waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
    
        public bool HasQueuedItem
        {
            get
            {
                lock(_processingQueue)
                {
                    return _processingQueue.Any();
                }
            }
        }
    
        public SomeObject NextQueuedItem
        {
            get
            {
                if ( !HasQueuedItem )
                    return null;
    
                lock(_processingQueue)
                {
                    return _processingQueue.Dequeue();
                }
            }
        }
    
        public void AddItem(SomeObject item)
        {
            lock(_processingQueue)
            {
                _processingQueue.Enqueue(item);
            }
            _waitHandle.Set();
        }
        public ThreadingSample()
        {
            _worker = new Thread(ProcessQueue);
            _worker.Start();
        }
    
        private void ProcessQueue()
        {
            while(!_workerTerminateSignal)
            {
                if ( !HasQueuedItem )
                {
                    Console.WriteLine("No items, waiting.");
                    _waitHandle.WaitOne();
                    Console.WriteLine("Waking up...");
                }
                var item = NextQueuedItem;
                if (item != null)   // Item can be missing if woken up when the worker is being cleaned up and closed.
                    Console.WriteLine(string.Format("Worker processing item: {0}", item.Data));
            }
        }
    
        public void Dispose()
        {
            if (_worker != null)
            {
                _workerTerminateSignal = true;
                _waitHandle.Set();
                if ( !_worker.Join( TimeSpan.FromMinutes( 1 ) ) )
                {
                    Console.WriteLine("Worker busy, aborting the thread.");
                    _worker.Abort();
                }
                _worker = null;
            }
        }
    
        public class SomeObject
        {
            public string Data
            {
                get;
                set;
            }
        }
    }
    

    Testing it I use a Unit test to kick it off. You can extend the unit test into a proper test to ensure that actions are being performed as expected. In my case they’re a good initial assertion to spike out behaviour.

            [Test]
        public void TestThreading()
        {
            using ( var sample = new ThreadingSample() )
            {
                sample.AddItem(new ThreadingSample.SomeObject {Data = "First Item"});
                sample.AddItem(new ThreadingSample.SomeObject {Data = "Second Item"});
                Thread.Sleep(50);
                sample.AddItem(new ThreadingSample.SomeObject {Data = "Third Item"});
            }
    
        }
    

    Relevant output from the test:

    —— Test started: Assembly: NHMapping.dll ——

    Worker processing item: First Item
    Worker processing item: Second Item
    No items, waiting.
    Waking up…
    No items, waiting.
    Waking up…
    Worker processing item: Third Item
    No items, waiting.
    Waking up…

    1 passed, 0 failed, 0 skipped, took 0.12 seconds (Ad hoc).

    Here you can see the worker going to sleep, then waking up to process items in the queue. Technically you can use a list, then fetch 10 items from the list before releasing it from the lock and process those 10 items before checking the list again.

    When the class is disposed it releases the loop then waits a moment for the worker thread to terminate before aborting. Here you’d probably want to check for any outstanding items and either log that they will not be processed, or persist them to file for later processing.

    Edit: I found the issue with the double-event… A better implementation would be to use a ManualReset on the EventWaitHandle

    private EventWaitHandle _waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
    

    Then when handling the case where you process an item, resent the handle:

                    var item = NextQueuedItem;
                if (item != null)   // Item can be missing if woken up when the worker is being cleaned up and closed.
                {
                    Console.WriteLine(string.Format("Worker processing item: {0}", item.Data));
                    _waitHandle.Reset();
                }
    

    This produces the better test results:

    —— Test started: Assembly: NHMapping.dll ——

    Worker processing item: First Item
    Worker processing item: Second Item
    No items, waiting.
    Waking up…
    Worker processing item: Third Item
    No items, waiting.
    Waking up…

    1 passed, 0 failed, 0 skipped, took 0.13 seconds (Ad hoc).

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

Sidebar

Related Questions

I have a list of data in javascript that looks like this: [[152, 48,
I have an excel sheet that has dde links to real time market data.
XML/XSLT Newbie here. I have some sets of data that need to be compared.
I have a list of data points (0.2, 0.8, 0.95) that I want to
I have a list of data that is a schedule. Each item has a
I have a long list of data that I want to display in table
I have some C structures related to a 'list' data structure. They look like
I need to keep a list of user_id s that have viewed a piece
I have two sets of data which I need to join, but there is
I have a large number of data sets each containing a long list of

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.