i have done some research but found nothing on how to run an event based on pc time.
I.e
Say for example a user selects 10pm every day for a file copy to take place how would you set that up in .net
Printable View
i have done some research but found nothing on how to run an event based on pc time.
I.e
Say for example a user selects 10pm every day for a file copy to take place how would you set that up in .net
Have you thought of using the Task Scheduler?
Hi,
If this is something which you need to do within your VB project then have a look at this, which is a modification of something I posted a few months ago. Read through the code to understand what is going on and then just replace my sample DateTimes within the lstActivationTimes List with the dates and times you need to check for. You will need to add a ListBox and a Timer to a Form for this demonstration to function correctly:-
As a side note, there have been numerous comments on this Forum as to the accuracy of a Timer over long periods of time so you may need to test this example and post back your own experiences of accuracy over long periods of time.Code:Public Class Form1
Dim lstActivationTimes As New List(Of DateTime)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim StartTime As DateTime = Now
'To demonstrate, set up a list of Times to be evaluated based on
' a StartTime. Just change this list to the times that need to be
'evaluated.
'Be sure to put the times in order of occurance since this
'routine does NOT accommodate times that are in the wrong order
With lstActivationTimes
.Add(StartTime)
.Add(StartTime.AddSeconds(2))
.Add(StartTime.AddSeconds(4))
.Add(StartTime.AddSeconds(6))
.Add(StartTime.AddSeconds(8))
.Add(StartTime.AddSeconds(10))
.Add(StartTime.AddMinutes(1))
End With
SetNextOccuranceOfTimer()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'Run the code that you need to be Executed Here
'You can get rid of everything BUT the SetNextOccuranceOfTimer routine
'Show what time the Timer Fired
ListBox1.Items.Add("Timer Fired @ " & Now.ToLongTimeString)
SetNextOccuranceOfTimer()
'Show the next Timer interval
ListBox1.Items.Add("Next Time Interval is " & (Timer1.Interval / 1000).ToString & " seconds")
End Sub
Private Sub SetNextOccuranceOfTimer()
Const millisecondsInSecond As Integer = 1000
Dim ListTimeUsed As Boolean
Dim currentTimeOfDay As TimeSpan = Now.TimeOfDay
'search the list of Times to be processed and set the next Timer interval
'for the Timer control correctly
For Each lstTime As DateTime In lstActivationTimes
If lstTime.TimeOfDay > currentTimeOfDay Then
Timer1.Interval = CInt(Math.Round(lstTime.TimeOfDay.Subtract(currentTimeOfDay).TotalSeconds, 0) * millisecondsInSecond)
ListTimeUsed = True
Exit For
End If
Next
'if the current time is greater than any given time in the list then
'we calculate the period between NOW and the First Time available in the List tomorrow
If Not ListTimeUsed Then
Dim EndOfDay As DateTime = DateTime.Parse("23:59:59 PM")
Timer1.Interval = CInt((Math.Round(EndOfDay.TimeOfDay.Subtract(currentTimeOfDay).TotalSeconds, 0) * millisecondsInSecond) + (Math.Round(lstActivationTimes.Item(0).TimeOfDay.TotalSeconds, 0) * millisecondsInSecond))
ListBox1.Items.Add("Timer Set to the First Time of the Next Day.")
End If
End Sub
End Class
Hope that helps.
Cheers,
Ian
Why dont you try something like this:
Usage: Run_OnDateTime(combobox1.selecteditem.text)Code:
Function Run_OnDateTime(ByVal userDate as Datetime)
Dim inputdate As Datetime
If date.now>=inputdate then
' Whatever you'd like to do goes here
End if
End Function
Please note that this has not been tested. This is how I'd approach the situation.
Hope it helps.