Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Threading.Timer and TimeSpan
Message
From
25/03/2009 15:33:51
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Threading.Timer and TimeSpan
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Miscellaneous
Thread ID:
01391484
Message ID:
01391484
Views:
86
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
Next
Reply
Map
View

Click here to load this message in the networking platform