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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:28:06+00:00 2026-05-22T03:28:06+00:00

I need a timer equivalent which will periodically execute some specific actions (e.g. updating

  • 0

I need a timer equivalent which will periodically execute some specific actions (e.g. updating some progress in the database or checking for new Jobs to execute in a database).

These actions are bound to a WaitHandle which specifies if the job needs to be executed or not. So basically this could be, for example, an AutoResetEvent which is set from outside when there is a new Entity in a database and trigger the search for these new Entities. The timer is necessary because I want to limit the number of queries to the database. So if 10 new notifications arrive, the AutomaticResetEvent will only be set one time and there will be also only one query for all of these.

My first attempts looks like this:

class ConditionalScheduler
{
    public void AddConditionalAction(WaitHandle handle, Action action)
    {
        lock (syncRoot)
        {
            handles.Add(handle);
            actions.Add(handle, action);
        }
    }

    private readonly object syncRoot = new object();

    private readonly Dictionary<WaitHandle, Action> actions = new Dictionary<WaitHandle, Action>();
    private readonly List<WaitHandle> handles = new List<WaitHandle>();

    public void RunTimerLoop()
    {
        do
        {
            WaitHandle[] waitHandles = handles.ToArray();
            int index = WaitHandle.WaitAny(waitHandles);

            if (index >= 0)
            {
                WaitHandle handle = waitHandles[index];

                Action action;
                lock (syncRoot)
                {
                    action = actions[handle];
                }

                action();
            }
            Thread.Sleep(5000);
        } while (true);
    }

The problem with this approach is that WaitHandle.WaitAny will only give me the Index of the first WaitHandle that is triggered. So if I have 2 or more WaitHandles which are triggered, then I will only execute the first actions and not the other ones.

Do you have a better design to achieve the required results of executing all actions which were triggered within the specified time period? If it simplifies the matter I could use another kind of notification mechanism instead of WaitHandles.

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-22T03:28:07+00:00Added an answer on May 22, 2026 at 3:28 am

    Maybe you could go with a two thread queue / bulk execution?
    Note that the code down is just a concept I’m writing without visual studio so I’m doing on a list, this had better to be done with a Queue but I don’t remember the syntax without intellisense 🙂 …

    private readonly Dictionary<WaitHandle, Action> actions = new Dictionary<WaitHandle, Action>();
        private readonly List<WaitHandle> handles = new List<WaitHandle>();
        private readonly List<WaitHandle> triggeredHandles = new List<WaitHandle>();
    
    // thread 1
        public void CheckLoop()
        {
            do
            {
                WaitHandle[] waitHandles = handles.ToArray();
                int index = WaitHandle.WaitAny(waitHandles);
    
                if (index >= 0)
                {
                    lock (triggeredHandles)
                    {
                       triggeredHandles.Add(waitHandles[index]);
                    }
    
                }            
            } while (true);
        }
    
    // thread 2
    public void RunLoop()
        {
            do
            {
                lock(triggeredHandles)
                {
                    foreach (var th in triggeredHandles) {
    
                       Action action;
                       lock (syncRoot)
                       {
                           action = actions[th];
                       }
    
                       action();                   
                    }
                    triggeredHandles.Clear();
                }
                Thread.Sleep(5000);
            } while (true);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a timer to execute callbacks with relatively low resolution. What's the best
I'm writing an app that will need to make use of Timer s, but
i have a input tag which is non editable, but some times i need
I need a timer tick with 1ms resolution under linux. It is used to
I need an accurate timer to interface a Windows application to a piece of
I use a System.Timers.Timer in my Asp.Net application and I need to use the
I'd like to have a java.utils.Timer with a resettable time in java.I need to
I'm writing (in C# with .NET 3.5) an administrative application which will poll multiple
I need to offer scheduling of actions/events, in our web site. A crap analogy
I need to solve the following question which i can't get to work by

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.