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

Time:09-16

I've just created queuing system with ASP.Net Webforms, using asp:Timer and asp:UpdatePanel to partial refresh the page. I set asp:Timer Interval to "100" which is means 0.1 second. After every partial page refresh, the page called Stored Procedures which is sometimes didn't run well. Are there any best practices of my problem and what's the impact on my system if I set Interval less than 1 second?

CodePudding user response:

When you have a timer which doesn't have enough time to complete before the next tick, a solution is to manually call the next tick inside the timer callback itself

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

The math ensures the next tick will either happen 100ms after the start of the current tick, or immediately after the current tick, accommodating ticks taking longer than 100ms.

Note: this should answer your question, but the comments below your question posing an additional question to you about your fundamental design should be seriously considered!

  • Related