Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Threading.Timer and TimeSpan
Message
From
27/03/2009 18:36:29
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
Application:
Desktop
Miscellaneous
Thread ID:
01391484
Message ID:
01391932
Views:
33
Hi Einar,

I think my problem comes from the two overloads are not consistent. With Timer.Change() there are if I remember right 4 different overloads. the first is Timer.Change(int dueTime, int Period) where the arguments represent milliseconds. I am allowed to pass in a Timeout.Infinite to prevent the timer from running.

The third overload allows the same thing but you can pass in TimeSpan objects instead of integers in milliseconds. So if I want the timer to be with say 60 seconds delay and 30 second period it is easier to pass in the TimeSpan objects. But if I want to essentially stop the timer, I need to pass in a Timeout.Infinite. That means I can't be consistent with how I call my SetTimer method because I can't seem to create a TimeSpan period with an infinite value of any sort.

So to stop the timer I have timer.Change(timeout.infinite, 60)
then to restart it I create TimeSpan objects and then pass those in.
Thanks a bunch
Tim

>Tim,
>Would -1 help you?
>See excerpt from M$ docs below
>
>dueTime
>Type: System..::.TimeSpan
>A TimeSpan representing the amount of time to delay before invoking the callback method specified when the Timer was constructed. Specify negative one (-1) milliseconds to prevent the timer from restarting. Specify zero (0) to restart the timer immediately. 
>
>period
>Type: System..::.TimeSpan
>The time interval between invocations of the callback method specified when the Timer was constructed. Specify negative one (-1) milliseconds to disable periodic signaling. 
>
>
>>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
Previous
Reply
Map
View

Click here to load this message in the networking platform