'Timer' stop ticking on lost focus

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi. I have created a 'stop watch' program.

The working principle of the program is to declare an integer (say
'intTime'), which is initalized to zero. Once the user clicks the 'Start'
button, the Timer component (the one found at System.Windows.Forms.Timer)
starts ticking (with this code: Timer.Enabled = True). Whenever the timer
ticks, 'intTime' is inclemented by 1 and this updated number is displayed on
the form. The interval of the Timer is 1000 so its ticks once a second.

The problem is when the window of the program lost focus for a while,
'intTime' freezes and never updates. (I have the TopMost property set to True
so I can see it even when it losts focus.)

How can I fix this problem? Thanks.
 
I tried this:

\\
Public Class Form1
Inherits System.Windows.Forms.Form
Dim intTimer As Int32 = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
intTimer = intTimer + 1
Label1.Text = intTimer
End Sub
End Class
//

And could not reproduce the effect of having the timer stop updating.
Not sure what is causing that. Although a better approach may be to do
something like this:

\\
Public Class Form1
Inherits System.Windows.Forms.Form
Dim dtTimer As Date

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
dtTimer = Now
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Label1.Text = DateDiff(DateInterval.Second, dtTimer,
Now).ToString
End Sub
End Class
//

This way you are not relying on the timer to tick every second, it can
go off a bit due to heavy CPU load or other reasons. This method will
make use of the system clock that is far more reliable.

Hope this helps.

Norst
 
I done basically the same & it works fine for me

Start a new Windows application
Add a label, timer & a button

Add this line as a declaration:

Dim intTime As Integer = 0

Double-click the button & add this code:

intTime = 0
Timer1.Enabled = True
Timer1.Interval = 1000
Label1.Text = 0

Double-click the timer & add this code:

intTime += 1
Label1.Text = intTime

Run it & it works perfectly

Crouchie1998
BA (HONS) MCP MCSE
 
Hi Poohface,

Thanks for replying ... I just want to know the purpose of this code:

DateDiff(DateInterval.Second, dtTimer,Now).ToString

I have yet to come across the function DateDiff in my life as a programer
.... would you mind explaining me the code syntax? Thanks very much.

Xero
 
Hi Poohface,

Thanks for replying ... I just want to know the purpose of this code:

DateDiff(DateInterval.Second, dtTimer,Now).ToString

I have yet to come across the function DateDiff in my life as a programer
.... would you mind explaining me the code syntax? Thanks very much.

Xero
 
Hi Poohface,

Thanks for replying ... I just want to know the purpose of this code:

DateDiff(DateInterval.Second, dtTimer,Now).ToString

I have yet to come across the function DateDiff in my life as a programer
.... would you mind explaining me the code syntax? Thanks very much.

Xero
 
DateDiff(DateInterval.Second, dtTimer,Now).ToString

what this does is gives us the difference between 2 dates that we pass
it.

Public Overloads Function DateDiff( _
ByVal Interval As DateInterval, _
ByVal Date1 As DateTime, _
ByVal Date2 As DateTime, _
Optional ByVal DayOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday,
_
Optional ByVal WeekOfYear As FirstWeekOfYear =
FirstWeekOfYear.Jan1 _
) As Long


Interval is how we want our response to be counted, in our case we
choose seconds, Date1 we gave it our dtTimer var that held the time
that we pressed our button at. for Date2 we gave it now() the current
time. So basically we asked it to give us the difference in seconds,
between the time we pressed our button and now. We also stated
..ToString to ensure that it did not give us a date type object back
but rather a string to put in our label.

Hope this helps,
Norst
 
Back
Top