how to execute if it has been 24 hours since last execution?

nickjonnes

New member
Joined
Jun 5, 2011
Messages
1
Programming Experience
3-5
hey all,
i am making a program and i would like it to execute my line of code every 24 hours. but if my computer has been turned off since then i want it to work out (when i turn it on) that it has been more than 24 hours and execute that line of code.
any ideas?
cheers nick
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,319
Location
Sydney, Australia
Programming Experience
10+
You would use My.Settings for that. Open the Settings page of the project properties and add a DateTime value. You can then do something like this:
Public Class Form1

    Private Const MILLISECONDS_PER_DAY As Integer = 24 * 60 * 60 * 1000

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim firstExecutionTime = My.Settings.LastExecutionTime.AddDays(1)

        If Date.Now > firstExecutionTime Then
            Me.Timer1.Interval = MILLISECONDS_PER_DAY
        Else
            Execute()
            Me.Timer1.Interval = Convert.ToInt32((firstExecutionTime - Date.Now).TotalMilliseconds)
        End If

        Me.Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If Me.Timer1.Interval <> MILLISECONDS_PER_DAY Then
            Me.Timer1.Stop()
            Me.Timer1.Interval = MILLISECONDS_PER_DAY
            Me.Timer1.Start()
        End If

        Execute()
    End Sub

    Private Sub Execute()
        '...
    End Sub

End Class
That may require some adjustment, depending on the circumstances, but you get the idea.
 
Top Bottom