Reputation: 135
my question here is how can i show a running timer consisting of minutes and seconds that is being imported from a SQL database. I have already imported it but then how can i display the timer in labels?
sqlq = "Select Duration from XYZ where ID = 20"
sda = New SqlDataAdapter(sqlq, conn)
sda.Fill(ds)
_timer = ds.Tables(0).Rows(0).Item("Duration")
Timer1.Interval = _timer * 60000
Timer1.Enabled = True
Timer1.Start()
Upvotes: 0
Views: 1536
Reputation: 5545
The Timer1_Tick event will fire every time the interval expires. IE, if ds.Tables(0).Rows(0).Item("Duration")
= 2, and you set the interval to be _timer * 60000
, since interval is in milliseconds, it will fire every 2 minutes.
This does not mean something is going to run for the next 2 minutes, it means the 2 minutes from now, the Timer1_Tick event will fire, than 2 minutes later it will fire again, and so on.
Seems like you are trying to run a timer, every second, for 2 minutes (in this case). You should set the interval to 1000 (1 second) and set some variable Private iCountDown as int32
in your form. The set it to the number of seconds you want it to run iCountDown = _timer * 60
. Now start your timer, Timer1.Start
.
Now, every send the tick event will fire. In this event you want to decrement the loop so you are counting down. iCountDown -= 1
and set your label text. Just make sure that when the countdown hits 0 that you stop the timer If iCountDown <= 0 then Timer1.Stop
One note, the timer runs on the UI thread so if you do not have any pumping (idle time) in your UI thread, the Tick even will not fire as expected.
Hope that helps you understand what is going on with timers.
Upvotes: 1