Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Starting and stopping a Threading.Timer
Message
 
À
01/10/2008 15:49:27
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Divers
Thread ID:
01352099
Message ID:
01352112
Vues:
26
>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform