Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Queued Timers

Queued Timers

Scheduled Pinned Locked Moved C#
designquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LiamD
    wrote on last edited by
    #1

    I am using the System.Threading.Timer timer. Now if I have a interval set quite low say 10ms it is possible for my timer event handler to be queued as they cannot be prcessed because the UI thread is busy or another application is running. When the UI thread becomes free it then processes all the queued events at the same time. Is it possible to determine programmatically the number of timer event handlers that are being queued. I want to be able to determine when there is a build up of queued event handlers. Some code to demonstrate this would be useful. Also, is it possible to limit the number of timer event handlers that are queued? Thanks, Liam

    G 1 Reply Last reply
    0
    • L LiamD

      I am using the System.Threading.Timer timer. Now if I have a interval set quite low say 10ms it is possible for my timer event handler to be queued as they cannot be prcessed because the UI thread is busy or another application is running. When the UI thread becomes free it then processes all the queued events at the same time. Is it possible to determine programmatically the number of timer event handlers that are being queued. I want to be able to determine when there is a build up of queued event handlers. Some code to demonstrate this would be useful. Also, is it possible to limit the number of timer event handlers that are queued? Thanks, Liam

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You could start a separate thread to do the actual work of the timer, that way the thread that is waiting for the event is never busy. This if course only works if the code is thread safe, as you could have several threads running at the same time. You can use a counter to see how many threads are running. Increase the counter when a task starts and decrease it when it finishes. If the code can't be made fully thread safe, you could queue the events yourself to be able to keep track of them. Still using a separate thread to do the work (to keep the main thread response to the events), but don't start a new thread until the previous finishes. --- b { font-weight: normal; }

      D 1 Reply Last reply
      0
      • G Guffa

        You could start a separate thread to do the actual work of the timer, that way the thread that is waiting for the event is never busy. This if course only works if the code is thread safe, as you could have several threads running at the same time. You can use a counter to see how many threads are running. Increase the counter when a task starts and decrease it when it finishes. If the code can't be made fully thread safe, you could queue the events yourself to be able to keep track of them. Still using a separate thread to do the work (to keep the main thread response to the events), but don't start a new thread until the previous finishes. --- b { font-weight: normal; }

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        I found the question a little odd considering the Timer he's using doesn't use events. It uses a callback delegate executed on a seperate thread out of the thread pool. So, my question would be, does each tick get its own thread? If so, then in theory, a 10ms time interval could exhaust the thread pool if the callback code takes more than 10ms to execute. What happens to the Timer then? Does the callback get queued up waiting for the thread pool? Does this queue have an upper limit...the size of the stack, maybe? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        L 1 Reply Last reply
        0
        • D Dave Kreskowiak

          I found the question a little odd considering the Timer he's using doesn't use events. It uses a callback delegate executed on a seperate thread out of the thread pool. So, my question would be, does each tick get its own thread? If so, then in theory, a 10ms time interval could exhaust the thread pool if the callback code takes more than 10ms to execute. What happens to the Timer then? Does the callback get queued up waiting for the thread pool? Does this queue have an upper limit...the size of the stack, maybe? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          L Offline
          L Offline
          LiamD
          wrote on last edited by
          #4

          OK my terminology may be wrong. I use the following so I use the the wording "event handler" tmrTimersTimer.Elapsed += new ElapsedEventHandler(tmrTimersTimerElapsedHandler); No each tick does not get it's own thread. The callback code takes about 1-2 ms so should complete in plenty of time. Now if another thread has the processor when my timer elapses it will queue. It will continue to queue timer callbacks. When my code gets the CPU back it will empty the queue and execute all the callbacks. What I was hoping to achieve was to interrogate the queue that holds the timer callbacks.

          D 1 Reply Last reply
          0
          • L LiamD

            OK my terminology may be wrong. I use the following so I use the the wording "event handler" tmrTimersTimer.Elapsed += new ElapsedEventHandler(tmrTimersTimerElapsedHandler); No each tick does not get it's own thread. The callback code takes about 1-2 ms so should complete in plenty of time. Now if another thread has the processor when my timer elapses it will queue. It will continue to queue timer callbacks. When my code gets the CPU back it will empty the queue and execute all the callbacks. What I was hoping to achieve was to interrogate the queue that holds the timer callbacks.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            OK. Now you've got me confused. Which timer are you using??? System.Threading.Timer - From your original post. Uses a callback delegate, not an event. System.Timers.Timer - Uses an event called Elapsed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            L 1 Reply Last reply
            0
            • D Dave Kreskowiak

              OK. Now you've got me confused. Which timer are you using??? System.Threading.Timer - From your original post. Uses a callback delegate, not an event. System.Timers.Timer - Uses an event called Elapsed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              L Offline
              L Offline
              LiamD
              wrote on last edited by
              #6

              Dave, sorry for the mixed up information I think I am confusing myself too. What I have found is both timers:- System.Threading.Timer System.Timers.Timer Will queue and "handler code" if it cannot be processed immediately. the "handler code" will then be processed when CPU time is avaliable. Whereas System.Windows.Forms.Timer Does not queue and the "handler code" is essentially lost. So I my question applies to both System.Threading.Timer System.Timers.Timer Can I find out if there is a queue of "handlers" (either a callback delegate or an event handler) waiting to be processed?

              D 1 Reply Last reply
              0
              • L LiamD

                Dave, sorry for the mixed up information I think I am confusing myself too. What I have found is both timers:- System.Threading.Timer System.Timers.Timer Will queue and "handler code" if it cannot be processed immediately. the "handler code" will then be processed when CPU time is avaliable. Whereas System.Windows.Forms.Timer Does not queue and the "handler code" is essentially lost. So I my question applies to both System.Threading.Timer System.Timers.Timer Can I find out if there is a queue of "handlers" (either a callback delegate or an event handler) waiting to be processed?

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Threading.Timers uses the ThreadPool to execute the callback code. You could try to call ThreadPool.GetMaxThreads and ThreadPool.GetAvailableThreads to see how many are in use, but I doubt it'll be that accurate. Timers.Timer uses Events to notify your app of the Timer Tick, I THINK by sending WM_TIMER messages to your apps message pump. You might try looking into GetQueueStatus[^] to see if it'll return the number of WM_TIMER messages you want. There is no queue of callbacks maintained anywhere. They are executed by the ThreadPool, which does not keep track of the source, or type, of callback to be executed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups