Page 1 of 1

Timer check without interupth the program gets slow or stuck

Posted: Sun Jan 08, 2017 11:31 am
by Dummy1912
Hello,

we like to get a timer that checks when the system date change
so we suppose to check it every hour or a complete day...
then to refresh our form

but no idea how to do it and even without to make the program slow or cpu overloading

Thanks.

Re: Timer check without interupth the program gets slow or s

Posted: Sun Jan 08, 2017 2:01 pm
by CodenStuff
Here's a snippet that is meant to run every hour, exactly on the hour:
Code: Select all
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim aTimer As New System.Timers.Timer(MillisecondsToNextTopOfTheHour)
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        aTimer.Start()
    End Sub

    Private Shared Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
        Dim aTimer As System.Timers.Timer = CType(source, System.Timers.Timer)
        aTimer.Stop()

        'Enter code here to change your calendar or whatever you need to happen each hour

        aTimer.Interval = MillisecondsToNextTopOfTheHour()
        aTimer.Start()
    End Sub

    Private Shared Function MillisecondsToNextTopOfTheHour() As Double
        Return DateTime.Today.Add(New TimeSpan(DateTime.Now.Hour, 0, 0)).AddHours(1).Subtract(DateTime.Now).TotalMilliseconds
    End Function
Should be easy to incorporate in to your code.

Re: Timer check without interupth the program gets slow or s

Posted: Sun Jan 08, 2017 2:16 pm
by Dummy1912
Okay Thanks Codenstuff

lets see what i can do with it ;)


Edit:

Just wonderful and no much cpu at all either
thanks ;)

Dummy