将计时器间隔设置为无限
我有一个方法将 BO Connection.AliveInterval
链接到 System.Timers.Timer
(.NET 2 )。
某些连接被管理为始终连接。
这种情况设置可以吗
if (myConnection.AliveInterval == Connection.TimeInfinite)
{
myTimer.Interval = double.PositiveInfinity;
}
?
我是否应该期望计时器会抛出异常或在 Elapsed 事件发生时上升?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您指的是在使用 System.Timers.Timer 的构造函数时,我在 MonoDevelop 和 VS 中的黄色工具提示中获得的有些误导性信息:
您可以获取类型为
Int32
、UInt32
或TimeSpan
的参数的此信息。有点令人惊讶的是,这些类型不提供名为 Infinite 的属性。您想要使用
Timeout.Infinite
以获得所需的结果。请注意,在线参考文献对其进行了正确的描述:
编辑:我刚刚意识到这不适用于设置器,仅适用于计时器的构造函数。因此,您最好的选择可能是使用
myTimer.Stop()
(如 @spender 所建议)或myTimer.Enabled = false
(如 @Bolu 所评论)。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
:You get this information for a parameter with the type
Int32
,UInt32
orTimeSpan
. Somewhat surprisingly, these types do not provide a property namedInfinite
.You want to use a
Timeout.Infinite
in order to get the desired result.Note that the online reference discribes it correctly:
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 ormyTimer.Enabled = false
as commented by @Bolu.为什么不使用
myTimer.Stop()
?Why not
myTimer.Stop()
?MSDN:
因此,如果启用模拟器,
PositiveInfinity
将引发异常。现在,解决方案是禁用计时器并将值设置为“PositiveInfinity”。启用计时器时,捕获
ArgumentException
并将间隔检查为PositiveInfinity
。MSDN:
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, catchArgumentException
and ceck the Interval toPositiveInfinity
.