|
-
Aug 23rd, 2004, 05:28 PM
#1
Thread Starter
New Member
Execute Code based on System Time
What is the easiest way to have a program written in VB.NET to execute at a precise time? I want to have my code execute when the exact time in seconds is reached on the system clock. For example if it was set to execute at 11 seconds it would do so at 12:00:11, 12:01:11, 12:02:11, 12:03:11 ect.. For my purposes the hour and minute are not relevant.
Is there some code that will compare a text entry (the time in seconds at which to execute) with the seconds on the system clock, and wait until the two match to execute?
I am very new to VB.NET and any help is really appreciated!!!
Max
-
Aug 24th, 2004, 12:39 AM
#2
Hyperactive Member
I think its possible ....
make a program that is always running on the memory containning a timer that compares the string you have with the system time ... like (if statement or something
My software never has bugs. It just develops random features.
I RATE, YOU RATE!!!
-
Aug 24th, 2004, 01:13 AM
#3
Junior Member
You could use the Time function and get the system time and say you have an text box with the second in it. Just see if the seconds from the time function equal eachother and if they do call your function. But then you would have to check the time every second. But if you had a timer in your project and if say the interval 1000 equals 1 second (I think it does) you could just figure out what time it is, like say it is 12:00:01 and you want your code to fire on 11 seconds just set the interval of the timer to 10000 then next time the timer fires it will call the function, then set the timer to 60000 so it will do it on the next minuet. In the texbox change function when the text changes call the time function and update your timer so it calls your code the first time (this is the first example the 10 seconds part) then after it fires set the timer to 1 min.
-
Aug 24th, 2004, 01:17 AM
#4
Junior Member
Have a text box named TextBox1
A Timer named Timer1
And put all of this in your form class (same place as the code for textbox1 and timer1 etc.)
This is the code for the keypress event
Code:
' RESTRICT THE TEXT BOX TO ONLY NUMERIC INPUT
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
' IF ENTER IS PRESSED UPDATE OUR TIMER INTERVAL
If Char.IsControl(e.KeyChar) Then
Dim MyTime As Date
MyTime = TimeOfDay
If MyTime.Second() = TextBox1.Text Then
' THE TIMES ARE EQUAL SO CALL THE TICK FUNCTION
' HAVE TO SET THE TIMER INTERVAL TO SOMETHING BIGGER THEN 0
' SO SET IT TO 1
Timer1.Interval = 1
ElseIf MyTime.Second() < TextBox1.Text Then
Timer1.Interval = (TextBox1.Text - MyTime.Second()) * 1000
ElseIf MyTime.Second() > TextBox1.Text Then
Timer1.Interval = (60 - (MyTime.Second() - TextBox1.Text())) * 1000
End If
End If
End Sub
This is the code for the timer
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
DisplayTheTime()
' SET THE INTERVAL TO ONE MINUET
' 60 SECONDS * 1000 = 60000
Timer1.Interval = 60000
End Sub
This is the code for your function or what it could look like (use for testing reasons)
Code:
' PLACE YOUR CODE HERE
Private Sub DisplayTheTime()
Dim MyTime As Date
MyTime = TimeOfDay
Label1.Text = MyTime
End Sub
Hope this helps
- DTD33inc
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|