如果我在 asp:Timer 中将 Interval 设置为小于 1 秒会发生什么?

What happened if I set Interval less than 1 second in asp:Timer?

我刚刚使用 ASP.Net Webforms 创建了排队系统,使用 asp:Timerasp:UpdatePanel 来部分刷新页面。我将 asp:Timer Interval 设置为“100”,即 0.1 秒。每次部分页面刷新后,调用存储过程的页面有时 运行 不好。如果我将 Interval 设置为小于 1 秒,是否有针对我的问题的最佳实践以及对我的系统有何影响?

当您的计时器没有足够的时间在下一次计时前完成时,一个解决方案是在计时器回调本身内部手动调用下一次计时

ReadOnly interval As Integer = 100
ReadOnly minIntervalBetweenCalls As Integer = 0
Private ReadOnly Timer2 As New System.Threading.Timer(AddressOf Timer2_Tick, Nothing, interval, -1)

Private Sub Timer2_Tick(state As Object)
    Dim nextRunTime = DateTime.Now.AddMilliseconds(interval)
    ' run processes
    Timer2.Change(Math.Max(nextRunTime.Subtract(DateTime.Now).Milliseconds, minIntervalBetweenCalls ), -1)
End Sub

数学确保下一次报价将在当前报价开始后 100 毫秒发生,或者在当前报价之后立即发生,以适应需要超过 100 毫秒的报价。