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