简体   繁体   English

使用 AutoReset = False 时停止后计时器仍在滴答作响

[英]Timer still ticking after stop when using AutoReset = False

I'm using a System.Timers.Timer in a service that seems to keep ticking after it has been disabled.我在一项服务中使用 System.Timers.Timer,该服务在禁用后似乎一直在滴答作响。 I'm using autoreset = false so I would expect it to only tick once unless I start it up again.我正在使用 autoreset = false 所以我希望它只会打勾一次,除非我再次启动它。 I'm calling stop again, just for good measure, but the timer keeps on ticking.我再次打电话停止,只是为了更好的措施,但计时器一直在滴答作响。 I'm using VB.NET in .net 1.1 and don't have the option to change frameworks.我在 .net 1.1 中使用 VB.NET 并且没有更改框架的选项。

Here is a simple console app to reproduce the issue.这是一个简单的控制台应用程序来重现该问题。 This is not the actual code from the project.这不是项目的实际代码。 I believe the interval is long enought to prevent any thread issues in raising the event multiple times, but I will admit I'm not a threading expert.我相信间隔足够长以防止多次引发事件时出现任何线程问题,但我承认我不是线程专家。

Module Module1
Private Timer As New System.Timers.Timer(10000)


Sub Main()
    AddHandler Timer.Elapsed, AddressOf Timer_Elapsed

    Timer.AutoReset = False
    Timer.Start()

    While True
        Console.ReadLine()
        Console.WriteLine("Timer Enabled: " & Timer.Enabled)
    End While

End Sub

Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    Console.WriteLine("Timer Tick")
    Timer.Interval = 15000
    Timer.Stop()  'using timer.enabled = false here nets the same effect

End Sub
End Module

You can see that the timer will keep on outputting to console indefinitely even after being stopped.您可以看到计时器即使在停止后也会无限期地继续输出到控制台。 You can press enter to check the value of the timer enabled to confirm that it is false while it is still ticking.您可以按回车键检查启用的计时器的值,以确认它仍在滴答作响时为假。 It has something to do with the interval being reset after it has ticked, but I don't understand why it isn't showing as enabled if setting the interval is supposed to turn it back on and why calling stop afterwards doesn't stop it.它与在勾选后重置间隔有关,但我不明白为什么如果设置间隔应该将其重新打开,为什么它没有显示为已启用以及为什么之后调用 stop 不会停止它.

I'm sure I'm just missing something simple, so any insight would be appreciated.我确定我只是缺少一些简单的东西,因此任何见解都将不胜感激。

Update更新

It seems that it is the combination of autoreset=false and resetting the interval after the tick.好像是autoreset=false和tick后重置间隔的组合。 If I set autoreset=true, resetting the interval doesn't cause problems.如果我设置 autoreset=true,重置间隔不会导致问题。 If I take out the interval reset, autoreset=false functions properly.如果我取消间隔重置,则 autoreset=false 功能正常。 I've tested this in .net 4 and the issue exists there as well.我已经在 .net 4 中测试过这个问题,那里也存在这个问题。 I've got a workaround for my project by using autoreset=true and manually turning off the timer immediately after elapsed, but I would really be interested to find out why exactly it works this way.通过使用 autoreset=true 并在计时器结束后立即手动关闭计时器,我为我的项目找到了解决方法,但我真的很想知道为什么它会以这种方式工作。

使用 Elapsed 事件时,应将Timer.Enabled属性设置为 false 以禁用计时器。

You might also read through this thread.你也可以通读这个线程。

Which .Net Timer() to use 使用哪个 .Net Timer()

I've had mixed results with System.Timers.Timer, the Windows.Forms.Timer seems to work much more intuitively.我用 System.Timers.Timer 得到了混合结果,Windows.Forms.Timer 似乎更直观地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM