开发者

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 ?


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.


Why not myTimer.Stop()?


MSDN:

The value must be greater than zero, and less than or equal to Int32.MaxValue

An ArgumentException will be thrown, when the interval is greater than Int32.MaxValue, and the timer is currently enabled. If the timer is not currently enabled, no exception is thrown until it becomes enabled.

So, the PositiveInfinity will throw an exception, if the simer will be enabled.

Now, a solution would be to disable the timer and set the value to PositiveInfinity. When Enabling the Timer, catch ArgumentException and ceck the Interval to PositiveInfinity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜