Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Starting and stopping a Threading.Timer
Message
From
01/10/2008 18:27:13
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
To
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:
01352151
Views:
22
>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

I tried to make this flexible for either starting or adjusting and also to handle both of my timers but not sure how to handle the Delegate method yet.
private void SetTimer(System.Threading.Timer timer, int delaySeconds, int intervalSeconds)
{
    if (timer == null)
    {
        using (AutoResetEvent autoEvent = new AutoResetEvent(true))
        {
            TimeSpan delayTime = new TimeSpan(0, 0, 0, delaySeconds, 0);
            TimeSpan timerInterval = new TimeSpan(0, 0, 0, intervalSeconds, 0);

            timer = new System.Threading.Timer(new TimerCallback(this.OnQTimerEvent), autoEvent,
	delayTime, timerInterval);
        }
    }
    else // Reset Timer
    {
        timer.Change(delaySeconds, intervalSeconds);
    }
}
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform