Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Starting and stopping a Threading.Timer
Message
From
01/10/2008 17:56:28
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01352099
Message ID:
01352145
Views:
20
>>I found the answer here:
>>http://www.yoda.arachsys.com/csharp/threads/timers.shtml
>>
>>I wish I had found this before I posted (bg).
>>Tim
>
>As yet another alternative, here's what I normally do:
>
>
>private void StartTimer()
>{
>    if (this.m_eventTimer == null)
>    {
>        using (AutoResetEvent autoEvent = new AutoResetEvent(true))
>        {                	
>            TimeSpan delayTime = new TimeSpan(0, 0, 0, 5, 0);
>            TimeSpan timerInterval = new TimeSpan(0, 0, 0, Config.TimerIntervalSeconds, 0);	                
>
>            this.m_eventTimer = new Timer(new TimerCallback(this.OnTimerEvent), autoEvent, delayTime, timerInterval);
>        }                	
>    }
>}
>
>private void StopTimer()
>{
>    if (this.m_eventTimer != null)
>    {
>        this.m_eventTimer.Dispose();	
>        this.m_eventTimer = null;                
>    }
>}
>
>private void OnTimerEvent(object stateInfo)
>{
>    this.StopTimer();
>    // More code
>}
>
>
>I can't this is any better or worse than any of the other alternatives (I've just been using this pattern for quite a while).

The only thing different for me is I don't want to dispose the timer when I stop it. Because I have a bit of processing to do on the timer interval, I want to suspend the timer while I do the processing and then start it back up again when I am done. I also may need to adjust the delay time if the previous service access wasn't successful so there is more time elapsed before trying again. The best I have come up with is to do a timer.Change(Timeout.Infinite, this.QCheckInterval); and then set a new time again after the processing is complete.
I like your startup and stop methods as they are pretty clean.
Tim
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform