How to constantly check for changes in system time? - vb.net

I'm trying to make a program that makes a sound every hour, how would I do this?
I thought maybe constantly checking the system time and if it's equal to either "1:00 2:00 3:00 etc..." then play a sound, but I don't know how to do this, some help would be appreciated :)

Add a Timer, set Interval for 1000 (1 second) and in Tick event check current time
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'check time here and put it in Label1
Label1.Text = TimeOfDay.ToString("hh:mm:ss")
'if currenttime = alarmtime then do something
If TimeOfDay.ToString("hh:mm:ss") = "10:01:00" Then Beep()
End Sub
For more info how to read current time:
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateandtime(v=vs.110).aspx

Create a timer, set the interval for 500 milliseconds (.5s), setting to 1 second could cause the tick to be missed. This will fire the tick event, check if the current time minutes and seconds are equal to zero, then fires. The timer is disabled between the sound firing in case the timer checks again within the second (because the timer is called ever half a second).
ReadOnly _tmr As New Timer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_tmr.Interval = 500
AddHandler _tmr.Tick, AddressOf tmr_Tick
_tmr.Enabled = True
End Sub
Private Sub tmr_Tick(sender As Object, e As EventArgs)
Dim curDateTime As DateTime = Now
If curDateTime.Minute = 0 And curDateTime.Second = 0 Then
_tmr.Enabled = False
PlaySound()
_tmr.Enabled = True
End If
End Sub
Private Sub PlaySound()
'// Play Sound
End Sub

Related

Message box were not showing when meeting desired minutes and seconds on timer

Could you help review what am I missing on the code. What i would want to achieve is to prompt a message box when the of every 49 minutes of each hour.
Already tried declaring the value of the minutes and seconds but it doesn't prompt a message when 49 minutes and 00 seconds where reach it only continous to tick
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Timer1.Start()
If lblMinutes.Text = "48:" And lblsecond.Text = "00" Then
MessageBox.Show("Please wait were updating the data table..")
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
lblHours.Text = Date.Now.ToString("hh:")
lblMinutes.Text = Date.Now.ToString("mm:")
lblsecond.Text = Date.Now.ToString("ss")
lbltt.Text = Date.Now.ToString("tt")
End Sub
I expect a message box everytime the timer reach the 49 minutes and 00 seconds of each hour
Private Sub Form_Load()
Timer1.Interval = 60000 '1 min
Timer1.Enabled = True
End Sub
Dim isec As Integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
isec += 1
If isec = 49 Then
MessageBox.Show("Please wait were updating the data table..")
isec = 0
End If
End Sub
Try This.
In regards to your code, the if statement in your button4 click event isn't firing constantly because it only goes through it once upon button click, so you click the button, timer starts, checks the label see if it "48:" then will exit the button click event and never check the label again unless you press the button again, understand? So maybe even put your whole "IF" statement into the Timer1.Tick event.
Events only occur when the trigger event happens. So in your case the IF statement in your button4 click event only is executed when the button is clicked. So unless you click the button on the exact time your looking for it won't display the message.

Application.DoEvents() CheckBox Bug (VB)

I was having problems running my application when (God Mode) was running... I found online, that it seems to be a common bug. The fix is to put Application.DoEvents() Within the loop, which I did... But now everytime I turn God Mode on, The CheckBoxes bug out, when they're clicked they activate... The program doesn't stop responding but there needs to be two clicks for the program to visually show that it's active:
Full Code:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
Do
Application.DoEvents()
WriteDMAInteger("Dishonored", GetModuleHandle("Dishonored", "Dishonored.exe") + &H100C810, {&H344}, 70, 1, 4)
Loop Until CheckBox1.Checked = False
End If
End Sub
Any ideas or solutions?
As the comments indicate, don't use Application.DoEvents() It's more trouble than it's worth. Instead, use a timer.
Drag a Timer control onto your form from the Toolbox. Set its Interval property to something suitable (like 100 ms). Add the Tick event and call your WriteDMAInteger method in that event. In your check box's changed event simply enable or disable the timer:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
'Enables the time if the checkbox is checked
Timer1.Enabled = CheckBox1.Checked
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
WriteDMAInteger("Dishonored", GetModuleHandle("Dishonored", "Dishonored.exe") + &H100C810, {&H344}, 70, 1, 4)
End Sub
With the timer's interval set to 100 ms, the WriteDMAInteger method will be called 10 times per second. You can experiment with a smaller value than 100 for the interval, but the limit will be about 50ms.

How to detect when a certain time has passed in VB.net

I need a way to finding out when the time from 0 ends up as 4 secs.
My code is as follows:
I have a global variable called weightdelaycount, which is incremented every 1000 intervals.
Private Sub Timer_weightcheck_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer_weightcheck.Tick
weightdelaycount = weightdelaycount + 1
End Sub
Now I have the do while loop that runs infinitely and only stops depending on two conditions. Either the weightchange = True or if the clock = 4 secs.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
weightdelaycount = 0
Timer_weightcheck.Enabled = True
Do While 1
If (weightchange() = True ) Then
Timer_weightcheck.Enabled = False
Exit Do
End If
If (weightdelaycount = 4) Then
Timer_weightcheck.Enabled = False
Exit Do
End If
Loop
MessageBox.Show(weightdelaycount)
End Sub
From the routine above you see that I'm using exit Do to exit the loop if the two conditions are met. The problem is that if the weightchange() is not True and 4 seconds passed the systems doesn't stop. I can put a delay in there and then it works, but I need to be accurate with the values that I get for the weight from a scale. If I put a delay, then the values will not be accurate. Is there a way to solve this?
Thank you in advance
Instead of using a timer, you could use a stopwatch. This code will loop until 4000ms after the stopwatch has started. You'll never get it to stop exactly on 4000ms because of the time it takes to run through the the Do..Loop(minimal really), but I presume a couple of ms after is close enough.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim weightdelaycount = 0
Dim timer As New Stopwatch
timer.Start()
Do
If (weightchange() = True) Then
timer.Stop()
Exit Do
End If
Loop Until timer.ElapsedMilliseconds > 4000
timer.Stop()
MessageBox.Show(timer.ElapsedMilliseconds)
End Sub

Add minutes to an existing countdown timer

how To add minutes to an existing countdown timer like while the timer is counting down I can add 10 mins with a push of a button so the time pluses like if the displayed time is 9.59 with the press of a button the time should display 19.59 and continue counting down
Public Class TIMER
Private alarmtime As Date
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If alarmtime < Date.Now Then
Me.Timer1.Stop()
MessageBox.Show("Time up")
Else
Dim remainingtime As TimeSpan = Me.alarmtime.Subtract(Date.Now)
Me.Label1.Text = String.Format("{0}:{1:d2}:{2:d2}", _
remainingtime.Hours, _
remainingtime.Minutes, _
remainingtime.Seconds)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Me.alarmtime = Date.Now.AddHours(TextBox1.Text)
Me.Timer1.Start()
ElseIf RadioButton2.Checked = True Then
Me.alarmtime = Date.Now.AddMinutes(TextBox1.Text)
Me.Timer1.Start()
ElseIf RadioButton3.Checked = True Then
Me.alarmtime = Date.Now.AddSeconds(TextBox1.Text)
Me.Timer1.Start()
End If
End Sub
Private Sub GameTIMER_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
its just a basic countdown timer it works fine all I need for it to do is add minutes to it while its is counting down because when the timer is up I have to click the text box type in the number then press ok then the timer starts so instead off writhing in the textbox I just want to press a button and the countdown timer automatically adds lets say 10 minutes to current time being displayed while its counting down
If you're talking about a normal Timer, this should be your answer:
Me.Timer1.Interval += (60000 * 10)
EDIT:
Try stopping it first:
Me.Timer1.Stop()
Me.Timer1.Interval += (60000 * 10)
Me.Timer1.Start()

Stop timer at a certain time in Visual Studio 2013

I am trying to play and stop the timer at a certain time that i typed in a textbox because i want to play an audio after the time is done.
Can you guys help me out?
Here is my code:
Private Sub Simulate_Click(sender As Object, e As EventArgs) Handles Simulate.Click
AxWindowsMediaPlayer1.Show()
AxWindowsMediaPlayer1.Ctlcontrols.play()
AxWindowsMediaPlayer1.Ctlcontrols.fastForward()
Simulate.Enabled = False
If TextBox1.Text = 1 Then
My.Computer.Audio.Play(My.Resources._1, AudioPlayMode.Background)
ElseIf TextBox1.Text = 2 Then
My.Computer.Audio.Play(My.Resources._2, AudioPlayMode.Background)
If i type say for example 2 seconds, it would take two seconds before the audio file plays.
Drop a timer control on the form and set it to the time interval where you would like it to stop at. In the timer's tick event disable the timer so that it doesn't fire again, and play your sound.
The timer will fire off X milliseconds after you set it's Enabled property to true.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
tmrAlarm.Interval = TimeSpan.FromSeconds(2).TotalMilliseconds
tmrAlarm.Enabled = True
End Sub
Private Sub tmrAlarm_Tick(sender As Object, e As EventArgs) Handles tmrAlarm.Tick
tmrAlarm.Enabled = False
MsgBox("Beep!")
End Sub
In this example a message box will pop up 2 seconds after pressing the button.
It requires a Timer control to be placed on the form named tmrAlarm.
This method will keep your application responsive without having to worry about complex multi-threading issues.
If you're not concerned about application responsiveness a simple System.Threading.Thread.Sleep(timeInSeconds * 1000) will do the trick. This will lock up the application for the sleep time.
If responsiveness is an issue you will need to have the playing of audio occur as a response to a new event you will create. Then you would need to start a new thread that sleeps and after sleep raises your new event.
You have to add a timer called "Timer1" for this to work. If textbox contains number "1" then time is set to 1000 milliseconds and timer is started, when 1000ms is up, then it will start playing the sound and then stop the timer.
Public Class Form1
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Contains("1") Then
Timer1.Interval = ("1000")
Timer1.Start()
ElseIf TextBox1.Text.Contains("2") Then
Timer1.Interval = ("2000")
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
My.Computer.Audio.Play(My.Resources._1, AudioPlayMode.Background)
End Sub
End Class