Count down timer in vb.net - vb.net

In the following code I have a timer that counts down from 5 mins. I am trying to have a visual count down timer in a lbl in mm:ss but the example I used doesn't work. It counts down but doesn't update the lbl until it hits 00:00.
The asker of the following question (were I got the code) said it works perfectly but for me it doesn't at all.
The Example I used
My code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
picLogo.SizeMode = PictureBoxSizeMode.StretchImage
'Timer until update
tmrUpdate.Interval = 300000 '5 minutes
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrUpdate.Enabled = True
End Sub
Private Sub tmrUpdate_Tick(sender As Object, e As EventArgs) Handles tmrUpdate.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTimer.Text = ts.ToString("mm\:ss")
Else
lblTimer.Text = "00:00"
tmrUpdate.Stop()
End If
End Sub

Answer:
Using a Async Sub I had the count down timer running while other stuff was going on in the back ground. This way the app could still be used during the Sub Wait() and this code also displayed the count down timer.
Used one timer on a 1 sec interval.
Private Async Sub DoStuff()
'Doing stuff
timeUpDate = 599
tmrUpdate.Start()
Application.DoEvents()
Await Task.Run(Sub()
Wait()
End Sub)
Loop
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles tmrUpdate.Tick
Dim hms = TimeSpan.FromSeconds(timeUpDate)
Dim m = hms.Minutes.ToString
Dim s = hms.Seconds.ToString
If timeUpDate > 0 Then
timeUpDate -= 1
lblTimer.Text = (m & ":" & s)
Else
tmrUpdate.Stop()
lblTimer.Text = "text"
End If
End Sub
Private Sub Wait()
Threading.Thread.Sleep(600000)
End Sub

Related

Vb.net How to Make the Timer countdown restart at 30 seconds when clicked on a new button in the same form

I'm trying to do a project and i'm still learning and this timer thing is something very new to me.
*You are required to build a board game. The game should be played on a 4 x 4 grid in pairs. Each grid cell contains a hidden question which the pair needs to answer.
The players take turns in playing the game. Each player generates two random numbers and place the cell on the grid indicated by the numbers. Once clicked, the question for the clicked grid cell is displayed. For example if the generated number is a 2 and a 3 the player clicks in the 2 x 3 grid cell to view the hidden question. You may use any kind of questions.
A player should only be able to click and view the question hidden beneath the grid cell represented by the random numbers.
At the start of the game, the initial score for each player should have a value of 0. For each question answered, the score increases by 5. You can display the score anywhere within the interface.
Each player should be given a duration of 30 seconds to answer a question. This value should be displayed on the user interface. The value should decrease with time and this decrement should be visible on screen at any point in time. Once the value reaches 0 the other player should be given the chance to answer the question. If both the players fail in answering a question, the question should be marked as unanswered. You can display the time left anywhere within the user interface.
The game should end when all the questions have been attempted by the players. When the game ends, the winner should be announced. The winner should be the one with the highest score.
Once the game is over, the questions and answers along with the players should be displayed.*
This is what its supposed to look like
This is what mine looks like
Now this is working just fine when i click on the 1st button even when they get the answer wrong or they go over time the timers will reset and countdown from 30 but when i click on the 2nd button the timer doesn't work even when i attached a second timer.
here is the code
Public Class Form1
Private TargetDT As DateTime
Private TargetDT1 As DateTime
Public CountDownFrom As TimeSpan = TimeSpan.FromSeconds(33)
Public CountDownFrom1 As TimeSpan = TimeSpan.FromSeconds(33)
Private TargetDTQ2 As DateTime
Private TargetDTQ2a As DateTime
Public CountDownFromQ2 As TimeSpan = TimeSpan.FromSeconds(33)
Public CountDownFromQ2a As TimeSpan = TimeSpan.FromSeconds(33)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
TargetDT1 = DateTime.Now.Add(CountDownFrom1)
TargetDTQ2 = DateTime.Now.Add(CountDownFromQ2)
TargetDTQ2a = DateTime.Now.Add(CountDownFromQ2a)
End Sub
Public Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
Dim ts1 As TimeSpan = TargetDT1.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime1.Text = ts.ToString("ss")
Else
lblTime1.Text = ":00"
Timer1.Stop()
MessageBox.Show("Out of Time!! Now player 2 can answer")
lblTime1.Text = ts1.ToString("ss")
Q1.TextBox1.Hide()
Q1.Button1.Hide()
Q1.TextBox2.Show()
Q1.Button2.Show()
End If
End Sub
Public Sub Timer2_Tick(sender As Object, e As System.EventArgs) Handles Timer2.Tick
Dim ts2 As TimeSpan = TargetDTQ2.Subtract(DateTime.Now)
Dim ts3 As TimeSpan = TargetDTQ2a.Subtract(DateTime.Now)
If ts2.TotalMilliseconds > 0 Then
lblTime1.Text = ts2.ToString("ss")
Else
lblTime1.Text = ":00"
Timer2.Stop()
MessageBox.Show("Out of Time!! Now player 2 can answer")
lblTime1.Text = ts3.ToString("ss")
Q1.TextBox1.Hide()
Q1.Button1.Hide()
Q1.TextBox2.Show()
Q1.Button2.Show()
End If
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
Q1.Show()
Timer1.Start()
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
Q2.Show()
Timer2.Start()
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
Application.Restart()
End Sub
End Class
This is my code for the second Button
Public Class Q2
Private TargetDT As DateTime
Public CountDownFrom As TimeSpan = TimeSpan.FromSeconds(36)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer3.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
End Sub
Public Sub Timer3_Tick(sender As Object, e As System.EventArgs) Handles Timer3.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
Form1.lblTime2.Text = ts.ToString("ss")
Else
'lblTime1.Text = ":00"
Timer3.Stop()
'MessageBox.Show("Out of Time!! Now player 2 can answer")
'Form1.lblTime1.Text = ts.ToString("ss")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Correctanswer As String
Correctanswer = ("Venus")
Dim result As Integer
result = String.Compare(TextBox1.Text, Correctanswer, True)
Dim result2 = String.Compare(TextBox2.Text, Correctanswer, True)
If result = 0 Then
MessageBox.Show("Bingo!")
Form1.txtP1.Text = Val(Form1.txtP1.Text) + 1
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Me.Close()
ElseIf result < 0 Then
MessageBox.Show("Wrong!")
TextBox1.Text = " "
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Form1.lblTime2.Show()
Timer3.Start()
TextBox1.Hide()
TextBox2.Show()
Button1.Hide()
Button2.Show()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Correctanswer As String
Correctanswer = ("Venus")
Dim result As Integer
result = String.Compare(TextBox1.Text, Correctanswer, True)
Dim result2 = String.Compare(TextBox2.Text, Correctanswer, True)
If result2 = 0 Then
MessageBox.Show("Bingo!")
Form1.txtP2.Text = Val(Form1.txtP2.Text) + 1
Form1.lblTime2.Text = ":00"
Form1.lblTime2.Hide()
Me.Close()
ElseIf result < 0 Then
MessageBox.Show("Wrong!")
TextBox1.Text = " "
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Form1.lblTime2.Show()
Me.Close()
End If
End Sub
End Class
PS. Also if someone can help explain how to change a buttons color from the code and not on click (This is so that the palyers can see what grid they have already clicked on)

Progress Bar increments at twice the intended speed each time

Private Time As New Timer
Private Sub btnWood_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWood.Click
prgWood.Value = 0
Time.Interval = 1000
Time.Start()
AddHandler Time.Tick, AddressOf IncreaseProgressBar
If prgWood.Value <> prgWood.Maximum Then
btnWood.Enabled = False
End If
Dim intAmountofWood As Integer = 11 * Rnd() + 10
intWood = intWood + intAmountofWood
Me.lblWoodAmount.Text = intWood
Private Sub IncreaseProgressBar(ByVal sender As Object, ByVal e As EventArgs)
prgWood.Increment(10)
If prgWood.Value = prgWood.Maximum Then
prgWood.Increment(0)
Time.Stop()
btnWood.Enabled = True
End If
End Sub
For my progress bar, I use a Timer to increment the value by 10 every 1 second. When I debug the project, it works fine the first time (taking 10 seconds for the progress bar to complete) but when I click the button a second time, it only takes 5 seconds, then less and less each time. This code is for an incremental game I'm trying to make for school.
From LarsTech's comment:
Public Class Form1
Private Time As New Timer
Public Sub New()
'Initialisation, etc
AddHandler Time.Tick, AddressOf IncreaseProgressBar
End Sub
'Other methods, etc
End Class
Then you need to remove the AddHandler from the button
click event
Can you do this?
Create a sub and add this code
`
ProgressBar1.Value = e.ProgressPercentage
If ProgressBar1.Value = ProgressBar1.Maximum Then
ProgressBar1.Value = ProgressBar1.Minimum
End If
`
then call that sub here.
`Private Sub btnWood_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWood.Click
'**Name of the Sub**
prgWood.Value = 0
Time.Interval = 1000
Time.Start()
AddHandler Time.Tick, AddressOf IncreaseProgressBar
If prgWood.Value <> prgWood.Maximum Then
btnWood.Enabled = False
End If
Dim intAmountofWood As Integer = 11 * Rnd() + 10
intWood = intWood + intAmountofWood
Me.lblWoodAmount.Text = intWood
End Sub
`
let see if that one works

Execute a function in a specific second in 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

a sub routine is not executing from another thread

i will be grateful if somebody will explain to me why this code that i wrote is not executing on another thread (the code just executing Button4_Click sub without any exception). if i`m calling timerclass() sub from the main thread everything is running perfect.
i cant understand this behavior.. maybe i need to use delegate(?)
Private t As System.Threading.Thread
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
t = New System.Threading.Thread(AddressOf timerclass)
t.Start()
End Sub
Private TargetDT As DateTime
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)
Private Sub timerclass()
tim = New Timer
AddHandler tim.Tick, AddressOf tim_Tick
tim.Interval = 500
tim.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
tim.Start()
End Sub
Private Sub tim_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
Label2.Text = ts.ToString("mm\:ss")
Else
Label2.Text = "00:00"
tim.Stop()
MessageBox.Show("Done")
End If
End Sub

How to pause a loop for 60 min after repeat 25 times?

I have a loop/for each here and I want to pause it for one hour after 25 repeats
Dim i As Integer
i = 0
For Each item In ListBox2.Items
i = i + 1
MessageBox.Show(item.ToString)
delay(1000)
Label5.Text = i
Next
Something like:
Dim i As Integer
Dim j As Integer
i = 0
j = 0
For Each item In ListBox2.Items
if(j!=25)
i = i + 1
MessageBox.Show(item.ToString)
delay(1000)
Label5.Text = i
j=j+1
else
delay(25*60*1000)
j = 0
Next
You should add a timer object to your form and set the interval to 3600000 (ms). Place your loop, that goes to 25 in a sub like Sub DoSomething(). In the Timer.Tick event handler you simply place another DoSomething() (and maybe stop the timer before DoSomething() and start the timer after, depending on how long DoSomething takes).
Then, when you want to start the task, call DoSomething() once manually and then start the timer. Every 60 minutes it will execute DoSomething().
This approach is much smoother, since your form will not be stuck in some loop or Thread.Sleep(). You should avoid this for any task that takes a significant amount of time.
It's furthermore easy to stop the task.
Public Class Form1
Private WithEvents TaskTimer As New System.Windows.Forms.Timer With {.Interval = 15000, .Enabled = False}
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
DoSomething()
End Sub
Private Sub DoSomething()
TaskTimer.Stop() 'Stop the timer while the task is being performed
For i = 1 To 25
MessageBox.Show("Hey ho yippieyahey")
Next
TaskTimer.Start() 'Restart the timer
End Sub
Private Sub TaskTimer_Tick(sender As Object, e As EventArgs) Handles TaskTimer.Tick
DoSomething()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
TaskTimer.Stop()
End Sub
End Class