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. Threading.Timer Problem

Threading.Timer Problem

Scheduled Pinned Locked Moved C#
helpquestioncsharp
3 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.
  • P Offline
    P Offline
    poqeqw
    wrote on last edited by
    #1

    Im writing a c# appliction that makes use of threading.timer. According to the msdn i can specify a period The time interval between invocations of callback , Does this means the timer will not t\ck until i finish handeling the tick event? I tried it out and I got two threads in the callback method… how can I solve this? Im using only one timer so there is no problem of more than one timer . Sample code will be greate help thanks

    S 1 Reply Last reply
    0
    • P poqeqw

      Im writing a c# appliction that makes use of threading.timer. According to the msdn i can specify a period The time interval between invocations of callback , Does this means the timer will not t\ck until i finish handeling the tick event? I tried it out and I got two threads in the callback method… how can I solve this? Im using only one timer so there is no problem of more than one timer . Sample code will be greate help thanks

      S Offline
      S Offline
      Skippums
      wrote on last edited by
      #2

      I beleive that the threading timer runs in its own thread, so you get a tick event every "interval" milliseconds, regardless of what processing you are doing. So if your interval is 20 msec, and it takes you 50 msec to perform the code in your event handler, then you will get more than one tick event processing at a time. I think this explains your comment, "I tried it out and I got two threads in the callback method", but am not certain. If you want the timer event handler to run in the main thread, try using a System.Windows.Forms.Timer, as it calls the handler on the main thread. I'm not sure what System.Timers.Timer does, but you could try to use that as well. Jeff

      D 1 Reply Last reply
      0
      • S Skippums

        I beleive that the threading timer runs in its own thread, so you get a tick event every "interval" milliseconds, regardless of what processing you are doing. So if your interval is 20 msec, and it takes you 50 msec to perform the code in your event handler, then you will get more than one tick event processing at a time. I think this explains your comment, "I tried it out and I got two threads in the callback method", but am not certain. If you want the timer event handler to run in the main thread, try using a System.Windows.Forms.Timer, as it calls the handler on the main thread. I'm not sure what System.Timers.Timer does, but you could try to use that as well. Jeff

        D Offline
        D Offline
        Darryl Borden
        wrote on last edited by
        #3

        I agree. Use the System.Windows.Forms.Timer. I've used a couple of options to keep the timer from firing while the previous timer has not completed. a) Turn off the timer at the beginning of the timer callback then turn it back on as you complete your callback processing (this will affect your interval time, though, so keep that in mind - if your timer is 60 seconds and it takes 2 seconds to process then your effective interval would be 62 seconds in this case) b) Use a "busy" variable. Set it to true at the start of the callback and false at the end. That way if a subsequent instance of the timer is fired before the previous one is completed then you can simply exit the callback without processing and wait until it fires again.

        Darryl Borden Principal IT Analyst dborden@eprod.com

        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