测试第一次引发 Elapsed 事件的时间。有效还是无用?
我有一个计时器,我想知道它是否有效,以测试计时器是否第一次引发经过事件(我想在第一次引发经过事件后更改 Interval 属性)
_timer = new Timer(1000);
_timer.Elapsed +=
(src, e) =>
{
FireTrigger(this, EventArgs.Empty);
if (isfirstTimeElapsed) // necessary ??
{
_timer.Interval = 5000
isfirstTimeElapsed = false;
}
};
在性能方面最好的选择是什么?测试与否?
I have a timer and I want to know if its efficient or not to test if it's the first time the timer raise the elapsed event or not (I want change the Interval property after the first time the Elapsed event is raised)
_timer = new Timer(1000);
_timer.Elapsed +=
(src, e) =>
{
FireTrigger(this, EventArgs.Empty);
if (isfirstTimeElapsed) // necessary ??
{
_timer.Interval = 5000
isfirstTimeElapsed = false;
}
};
In term of performances what's the best choice ? test or not ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它当然不是无用的,它是自记录代码,这意味着任何阅读它的人都会立即明白您想要更改第一次运行的间隔。如果您跳过 if 并只是每次设置间隔,那么您这样做的原因可能并不明显。就效率而言,我怀疑您会注意到差异,但当然,如果您担心,请执行一些测试。
Its certainly not useless, it is self documenting code that means anybody reading it immediately understands that you want to change the interval on the first run. If you skipped the if and just set the interval every time it might not be obvious why you were doing this. As far as efficiency is concerned I doubt you'd notice a difference but of course if you are worried then perform some tests.
您可以使用System.Threading.Timer,在其中您可以设置何时第一个事件应该被触发,其他事件
You can use System.Threading.Timer, in which you can set when first event should be fired, and the other ones