Question Using DateTimePicker with Timer

sam991

New member
Joined
Feb 16, 2011
Messages
2
Programming Experience
Beginner
Hi All,

Apologies for the noobish question - i'm utilising a couple of controls to kick off a process within my form - in short i have a DateTimePicker, Timer, CheckBox. The user enters the time they wish the process to run in the DateTimePicker (which has its format set to Time), ticks the CheckBox, which kicks off the Timer with an interval of 900ms. The Timer_Tick event checks if the DateTimePicker.Value is now and if so, does something. My problem is that either the DateTimePicker.Value never equals now or that the Timer_Tick event is never actually called.

I welcome any input into better ways of accomplishing this, i am conscious that polling for the time every second is a drain of resources but the only alternative i can see is a service which is overkill.

Code as follows:

VB.NET:
    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
        If ScheduleDateTimePicker.Value = Now Then
            ProcessEnabledCheckBox.Checked = True
        End If
    End Sub

    Private Sub EnableScheduledRunCheckBox_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnableScheduledRunCheckBox.CheckStateChanged
        If EnableScheduledRunCheckBox.Checked = True Then
            Timer.Enabled = True
            Timer.Start()
        ElseIf EnableScheduledRunCheckBox.Checked = False Then
            Timer.Stop()
            Timer.Enabled = False
        End If
    End Sub
 
the DateTimePicker.Value never equals now
That is true. If Date.Now >= scheduled time then task is due.
 
Hi John,

Yes that's true, but the problem with writing it as:

If time has passed then
do something

Is that the task will always try to rerun, even if it just finished. I want to enable the user to enter the time of their choice in DateTimePicker and then the process will run every 24 hours. Is this feasible?
 
Yes, but that would be different times, do understand that the date is part of the time. Once the schedules time is reached you can AddDays(1) to get the next scheduled time.
 
Back
Top