Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Starting and stopping a Threading.Timer
Message
 
To
01/10/2008 15:49:27
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:
01352112
Views:
27
>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).
-Paul

RCS Solutions, Inc.
Blog
Twitter
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform