Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Threading.Timer and TimeSpan
Message
De
25/03/2009 15:33:51
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Threading.Timer and TimeSpan
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Divers
Thread ID:
01391484
Message ID:
01391484
Vues:
87
Hi All,

I have a method I use to Set/ Reset my timers. I often need to set the first argument to infinity to stop the timer and then later set it back to a value to start it up again. My method creates a TimeSpan object to pass to the timer for both arguments of time. The below method works fine except when I pass in Infinite like this. I suppose a workaround is for me to pass in a really big value to create the TimeSpan with as the only reason I am stopping the timers is so I can do some processing before the timer fires again.
// Call the set timer to stop it.  the TimeSpan seems to get created OK, but the timer.Change() fails because of the infinite value.
this.SetTimer(tmrValidator, Timeout.Infinite, 60, null);

private void SetTimer(System.Threading.Timer timer, int delaySeconds, int intervalSeconds, TimerCallback callback)
{
     TimeSpan delayTime = new TimeSpan(0, 0, delaySeconds);
     TimeSpan intervalTime = new TimeSpan(0, 0, intervalSeconds);

     if (timer == null)
     {
          using (AutoResetEvent autoEvent = new AutoResetEvent(true))
          {
					
               if (callback == null)
                    callback = new TimerCallback(this.OnBlankTimerEvent);

               timer = new System.Threading.Timer(callback, autoEvent,
               delayTime, intervalTime);
          }
     }
     else // Reset Timer
     {
          timer.Change(delayTime, intervalTime);
     }
}
Timothy Bryan
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform