[RESOLVED] Help checking date and hour
Hello! Is there any way to check date and hour without using two Timers?
Right now i have this for the date:
Code:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
If Date.Today = My.Settings.SelectedDate Then
'Do Something
End If
End Sub
And for the time I don't know how to do it.... How can I do this using a Background Worker?
Thanks in advance!
PS: the Timer1.Interval = 3600000 miliseconds... That's one whole hour.
PS (again): I thought about setting the Timer1 interval to 60000miliseconds, so that it would check the time everysingle step of the way (or maybe 10 minutes...), but I don't want the app to lag, so... How can i implement a Background Worker?
Re: Help checking date and hour
Is there some reason why you can't use the NOW object?
Plus, I have no idea why you would need two timers to check a date and time.
Re: Help checking date and hour
Why not use the Windows Task Scheduler to schedule your application to launch at whatever interval you need to do.
Re: Help checking date and hour
I would probably set the (singular) timer to 1 second interval. In the tick event, check the 'now' time as Campion mentioned.
Very rarely is there a necessity for two forms-based timers.
Re: Help checking date and hour
Quote:
Originally Posted by
Pradeep1210
Why not use the Windows Task Scheduler to schedule your application to launch at whatever interval you need to do.
How do I use the windows task scheduler?
EDIT:
Ok, so i found this code that jmcilhinney posted 4 years ago...
Code:
Private Form1_Load(...) Handles MyBase.Load
Me.SetAlarm()
End Sub
Private Sub Timer1_Tick(...) Handles Timer1.Tick
'Set the alrm to Tick again at 2:00 PM tomorrow.
Me.SetAlarm()
'Do something here.
End Sub
Private Sub SetAlarm()
Dim currentTime As Date = Date.Now
Dim alarmTime As Date = Date.Today.AddHours(14)
'If it is already past 2:00 PM set the alarm for tomorrow.
If currentTime > alarmTime Then
alarmTime = alarmTime.AddDays(1)
End If
Dim timeToAlarm As TimeSpan = alarmTime.Subtract(currentTime)
'Set the Timer's Interval.
Me.Timer1.Interval = CInt(Math.Floor(timeToAlarm.TotalMilliseconds))
End Sub
So I change a line from Private Sub SetAlarm() to this:
Code:
Dim alarmDay As Date = DateTimePicker1.Value
If currentTime > alarmTime Then
alarmTime = alarmTime.AddDays(alarmDay.ToOADate())
End If
And I just wanted to know that if I select any day, let's say next week's wednesday, the alarm time value would change to "alarmtime + time to selected date". Or how could I calculate the days left until the alarm sets off?
Thanks!
EDIT 2:
Nevermind, I think I answered myself. I'll post code later.