将计时器间隔设置为无限

发布于 2024-09-27 02:51:30 字数 359 浏览 0 评论 0原文

我有一个方法将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惜醉颜 2024-10-04 02:51:30

也许您指的是在使用 System.Timers.Timer 的构造函数时,我在 MonoDevelop 和 VS 中的黄色工具提示中获得的有些误导性信息:

将该值设置为无限毫秒以禁用周期性信号发送。

您可以获取类型为 Int32UInt32TimeSpan 的参数的此信息。有点令人惊讶的是,这些类型不提供名为 Infinite 的属性。

您想要使用 Timeout.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:

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.

爱冒险 2024-10-04 02:51:30

为什么不使用myTimer.Stop()

Why not myTimer.Stop()?

十秒萌定你 2024-10-04 02:51:30

MSDN

该值必须大于零,
且小于或等于
Int32.MaxValue

将抛出ArgumentException
当间隔大于
Int32.MaxValue,计时器是
目前已启用。如果定时器是
当前未启用,也不例外
抛出直到它被启用。

因此,如果启用模拟器,PositiveInfinity 将引发异常。

现在,解决方案是禁用计时器并将值设置为“PositiveInfinity”。启用计时器时,捕获 ArgumentException 并将间隔检查为 PositiveInfinity

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文