Execute a function in a specific second in VB.NET - vb.net

I'm making a backup scheduler of my files, using a Timer, and it executes when the said time is equal to the current time, But my problem is it keeps on executing until 1 sec has passed like what i do when i use MsgBox.
This is what i have tried and it's not working, it still executes until 1 second has passed. Please help me achieve that. Thanks!
Dim d As Date = DateTime.Now
Dim d1 As Date = d.AddMilliseconds(1)
Dim dw As String = d1 & DateTime.Now.DayOfWeek.ToString
Dim date2 As Date = cntrl.Value.AddMilliseconds(1)
If Form3.chkWed.Checked = True Then
If dw = date2 & Form3.chkWed.Text Then
CopyStart(src, dest)
End If
End If
If Form3.chkThu.Checked = True Then
If dw = date2 & Form3.chkThu.Text Then
MsgBox("P")
End If
End If

What I'm talking about is something like the following. In this example, there are three RadioButton controls that indicate that a task should be initiated at 1.00 PM, 5.00 PM and 9.00 PM respectively.
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
'The next task is to start at 1.00 PM
Me.ResetTimerInterval(TimeSpan.FromHours(13))
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
'The next task is to start at 5.00 PM
Me.ResetTimerInterval(TimeSpan.FromHours(17))
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
'The next task is to start at 9.00 PM
Me.ResetTimerInterval(TimeSpan.FromHours(21))
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'The next task will start in 24 hours.
With Me.Timer1
.Stop()
.Interval = Convert.ToInt32(TimeSpan.FromHours(24).TotalMilliseconds)
.Start()
End With
'Start a task right now.
Me.InitiateTask()
End Sub
Private Sub ResetTimerInterval(nextTaskStartTime As TimeSpan)
Dim nextTaskStartDateTime = Date.Today + nextTaskStartTime
If Date.Now > nextTaskStartDateTime Then
nextTaskStartDateTime.AddDays(1)
End If
Dim timeUntilNextTask = nextTaskStartDateTime - Date.Now
With Me.Timer1
.Stop()
.Interval = Convert.ToInt32(timeUntilNextTask.TotalMilliseconds)
.Start()
End With
End Sub
Private Sub InitiateTask()
'...
End Sub

I have added another Timer, and did something like this.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim dw As String = DateTime.Now & DateTime.Now.DayOfWeek.ToString
If Form3.chkThu.Checked = True Then
If dw = Form3.DateTimePicker1.Value & Form3.chkThu.Text Then
Timer2.Enabled = True
End If
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Timer2.Enabled = False
MsgBox("P")
End Sub

Related

How to add DateTimePicker value to Current Time

I'm having a difficult time with this. The program is an Alarm clock that takes a user input duration of time and adds the current time to get a label of the future time of when the alarm is finished.
I have accomplished removing hardcoded hours but I can't add the current time to the duration
Option Strict On
Public Class AlarmTimerFRM
Private setTime As Date
Private Sub AlarmTimerFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
outCurrentTimeLBL.Text = Date.Now.ToLongTimeString
outCurrentTimeLBL.BackColor = Color.LightGray
Timer1.Start()
setTimerDTP.Format = DateTimePickerFormat.Custom
setTimerDTP.CustomFormat = "HH:mm:ss"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
outCurrentTimeLBL.Text = TimeOfDay.ToString("hh:mm:ss tt")
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
outCurrentTimeLBL.Text = TimeOfDay.ToString("hh:mm:ss tt")
End Sub
Private Sub SetBTN_Click(sender As Object, e As EventArgs) Handles SetBTN.Click
REM sets the alarm time
Dim OneHourAgo As DateTime
Dim FormattedTime As String
OneHourAgo = Now.AddHours(-1)
FormattedTime = OneHourAgo.ToString("HH:mm:ss")
AlarmTimeLBL.Text = (FormattedTime)
End Sub
Private Sub setTimerDTP_ValueChanged(sender As Object, e As EventArgs) Handles setTimerDTP.ValueChanged
End Sub
Private Sub ResetBTN_Click(sender As Object, e As EventArgs) Handles ResetBTN.Click
REM Resets the alarm time LBL
AlarmTimeLBL.Text = ""
End Sub
Private Sub AlarmTimeLBL_Click(sender As Object, e As EventArgs) Handles AlarmTimeLBL.Click
End Sub
End Class
Your "setTime" variable should be a DateTime so that it includes the date. This makes it easier to deal with current times and durations that span midnight.
Here's what it would like:
Private setTime As DateTime
Private Sub AlarmTimerFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
UpdateCurrentTime()
outCurrentTimeLBL.BackColor = Color.LightGray
Timer1.Start()
setTimerDTP.Format = DateTimePickerFormat.Custom
setTimerDTP.CustomFormat = "HH:mm:ss"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
UpdateCurrentTime()
End Sub
Private Sub UpdateCurrentTime()
outCurrentTimeLBL.Text = DateTime.Now.ToString("hh:mm:ss tt")
End Sub
Private Sub SetBTN_Click(sender As Object, e As EventArgs) Handles SetBTN.Click
setTime = DateTime.Now.Add(setTimerDTP.Value.TimeOfDay)
AlarmTimeLBL.Text = setTime.ToString("hh:mm:ss tt")
SetBTN.Enabled = False
setTimerDTP.Enabled = False
Timer2.Start()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If DateTime.Now >= setTime Then
Timer2.Stop()
MessageBox.Show("Alarm time reached!")
SetBTN.Enabled = True
setTimerDTP.Enabled = True
End If
End Sub
You could plus DateTime.Now with User Input Time.
Dim UserTime As String()
UserTime = Duration.Text.Split(':')
AlarmTimeLBL.Text = DateTime.Now.AddHours(UserTime(0)).AddMinutes(UserTime(1)).AddSeconds(UserTime(2))

Timer.tick within 1 min back-forth

I have 2 Form and i want it to be slide show back & forth within 1 minute.
Please write down the code thank you!
My Code:
Form 1
Private Sub BunifuCheckbox1_OnChange(sender As Object, e As EventArgs) Handles BunifuCheckbox1.OnChange
If BunifuCheckbox1.Checked Then
tmrSLIDE.Enabled = True
tmrSLIDE.Start()
Else
tmrSLIDE.Stop()
End If
End Sub
Private Sub tmrSLIDE_Tick(sender As Object, e As EventArgs) Handles tmrSLIDE.Tick
trs1.ShowSync(Form2)
Me.Hide()
End Sub
Form 2:
Private Sub BunifuCheckbox1_OnChange(sender As Object, e As EventArgs) Handles BunifuCheckbox1.OnChange
If BunifuCheckbox1.Checked Then
tmrSLIDE2.Enabled = True
tmrSLIDE2.Start()
Else
tmrSLIDE2.Stop()
End If
End Sub
Private Sub tmrSLIDE_Tick(sender As Object, e As EventArgs) Handles tmrSLIDE2.Tick
trs2.ShowSync(Form1)
Me.Hide()
End Sub
Property tmrSLIDE interval = 60000
property tmrSLIDE2 interval = 120000
I just Want to have 1 Checkbox with a simplified code thank you!

I have this countdown timer and i want it to start from a number set in a variable

I have this count down timer and i want it to start to count down from a value given in a variable.
Public Class frmSinglePlayer
Private TargetDT As DateTime
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmrCountdown.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrCountdown.Start()
End Sub
Private Sub tmrCountdown_Tick(sender As Object, e As System.EventArgs) Handles tmrCountdown.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime.Text = ts.ToString("mm\:ss")
Else
lblTime.Text = "00:00"
tmrCountdown.Stop()
MessageBox.Show("Done")
End If
End Sub
End Class
I want to change instead of the number 3 in this line, to put the value from my variable.
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)
I have 3 radiobuttons with different values.
Private Sub rd_1h_CheckedChanged(sender As Object, e As EventArgs) Handles rd_1h.CheckedChanged
ho = 1
End Sub
Private Sub rd_2h_CheckedChanged(sender As Object, e As EventArgs) Handles rd_2h.CheckedChanged
ho = 2
End Sub
Private Sub rd_3h_CheckedChanged(sender As Object, e As EventArgs) Handles rd_3h.CheckedChanged
ho = 3
End Sub
If is confusing , please ask me and sorry for my bad english.
Maybe you need something like this?
Private TargetDT As DateTime
Dim ho As Integer = 1
Private Sub StartTimerTick(sender As Object, e As System.EventArgs, Optional countDownFrom As Integer = 0)
If sender Is Nothing Then
TargetDT = DateTime.Now.Add(TimeSpan.FromMinutes(countDownFrom))
Dim mTmr = New Timer
mTmr.Interval = 500
AddHandler mTmr.Tick, AddressOf StartTimerTick
mTmr.Start()
Exit Sub
End If
Console.WriteLine("Tick ")
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime.Text = ts.ToString("mm\:ss")
Else
lblTime.Text = "00:00"
CType(sender, Timer).Stop()
CType(sender, Timer).Dispose()
MessageBox.Show("Done")
End If
End Sub
Private Sub rd_1h_CheckedChanged(sender As Object, e As EventArgs) Handles rd_1h.CheckedChanged,
rd_2h.CheckedChanged,
rd_3h.CheckedChanged
Select Case CType(sender, Control).Name
Case "rd_1h"
ho = 1
Case "rd_2h"
ho = 2
Case "rd_3h"
ho = 3
End Select
'StartTimerTick(Nothing, Nothing, ho)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
StartTimerTick(Nothing, Nothing, ho)
End Sub

Enable a counter at a specific time everyday

I setup a counter which needs to be enabled at specific time everyday. for an example lets say at (3 PM) everyday. what i came up with is following piece of code. but it gives me an error when it reach the time saying parameter is not valid please help me,
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
Execute()
End Sub
Private Sub Execute()
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Try this, add a timer to your program and call it CheckTimer and update your code like this :
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
CheckTimer.Interval = 1
CheckTimer.Start
End Sub
Private Sub CheckTimer_Tick(sender As Object, e As EventArgs) Handles CheckTimer.Tick
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Hope it helped :)

VB.net real-time time elapsed feature

I already have created a real time clock that synchronizes with the computer time and is being displayed in a label.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
I want to make a real-time time elapsed feature that keeps on counting the seconds/minutes/hours elapsed from the time it started till the time it stops and it would be basing on the real-time clock i have created. I would be creating a start and stop button for this. Is this possible? Thanks in advance.
I am now able to complete everything and i added a feature that records the starting and ending time based on my real time clock. Here is my working code:
Dim hr, min, sec As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Start.Text = ""
EndLbl.Text = ""
Elapse.Text = ""
Timer2.Enabled = True
Start.Text = Time.Text
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
sec = sec + 1
If (sec = 60) Then
sec = 0
min = min + 1
ElseIf (min = 60) Then
min = 0
hr = hr + 1
ElseIf (hr = 24) Then
hr = 0
min = 0
sec = 0
End If
Elapse.Text = String.Format("{0}hr : {1}min : {2}sec", hr, min, sec)
Timer2.Interval = 1000
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Timer2.Enabled = False
EndLbl.Text = Label4.Text
hr = 0
min = 0
sec = 0
Timer2.Interval = 1
End Sub
Credits to the starting code given by NeverHopeless. Thanks alot.
I suggest you use only 1 timer:
Public Class Form2
Private _elapseTimerRunning As Boolean = False
Private _elapseStartTime As DateTime
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
txtTime.Text = Now.ToString("h:mm:ss tt")
If _elapseTimerRunning = True Then
Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
txtElapsed.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
End If
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
_elapseStartTime = DateTime.Now
_elapseTimerRunning = True
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
_elapseTimerRunning = False
End Sub
End Class
An example for displaying the elapsed time the application has run.
Public Class Form1
'shows elapsed time that the app has run
'
Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000)
Dim stpw As Stopwatch = Stopwatch.StartNew
Private Sub tick(state As Object)
If stpw.IsRunning Then
'format - http://msdn.microsoft.com/en-us/library/ee372287.aspx
Me.Invoke(Sub()
Label1.Text = stpw.Elapsed.ToString("d\ \ hh\:mm\:ss\.ff")
End Sub)
End If
End Sub
End Class
To add start/stop functionality using buttons:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'start the elapsed timer
stpw.Start() 'continue
'or
'stpw.Restart() 'restart
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
stpw.Stop()
End Sub
Something like this would help you: (untested, but will give you a starter)
Dim hr, min, sec As Integer 'msec;
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'msec++;
'if(msec == 60) { msec = 0; sec++; }
sec+=1;
if(sec = 60) Then
sec = 0 : min+=1
end if
if(min = 60) Then
min = 0 : hr+=1
end if
if(hr = 24) Then
hr = 0 : min = 0 : sec = 0
end if
'TimeElapsed.Text = String.Format("{0}:{1}:{2} {3}", hr, min, sec, msec)
TimeElapsed.Text = String.Format("{0}:{1}:{2}", hr, min, sec)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
NOTE: Timer2 will run for every second.