Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Timer Interval to Infinite

I have a method that links a BO Connection.AliveInterval to a System.Timers.Timer (.NET 2).

Some connections are managed to be always connected.

Is it OK to set in such a case

if (myConnection.AliveInterval == Connection.TimeInfinite)
{
    myTimer.Interval = double.PositiveInfinity;
}

?

Should I expect that the Timer will throw exceptions or rises ever the Elapsed event ?

like image 854
serhio Avatar asked Aug 28 '26 21:08

serhio


1 Answers

Maybe you are referring to the somewhat misleading information I get in a yellow tooltip in MonoDevelop and VS when using the constructor of a System.Timers.Timer:

Set the value to Infinite milliseconds to disable periodic signaling.

You get this information for a parameter with the type Int32, UInt32 or TimeSpan. Somewhat surprisingly, these types do not provide a property named Infinite.

You want to use a Timeout.Infinite in order to get the desired result.

Note that the online reference discribes it correctly:

Specify Timeout.Infinite to prevent the timer from starting.

Edit: I just realized that this will not work for the setter, only for the constructor of the timer. So your best bet is probably to use myTimer.Stop(), as suggested by @spender or myTimer.Enabled = false as commented by @Bolu.

like image 181
Felix Alcala Avatar answered Aug 31 '26 12:08

Felix Alcala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!