471,596 Members | 830 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

'Timer' stop ticking on lost focus

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.

--
Xero

http://www.chezjeff.net
My personal web portal
Nov 21 '05 #1
6 3133
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

On Sat, 23 Apr 2005 07:13:01 -0700, "Xero" <jeff_at_chezjeff_dot_net>
wrote:
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.

Nov 21 '05 #2
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
Nov 21 '05 #3
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
"Poohface" wrote:
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

On Sat, 23 Apr 2005 07:13:01 -0700, "Xero" <jeff_at_chezjeff_dot_net>
wrote:
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.


Nov 21 '05 #4
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
"Poohface" wrote:
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

On Sat, 23 Apr 2005 07:13:01 -0700, "Xero" <jeff_at_chezjeff_dot_net>
wrote:
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.


Nov 21 '05 #5
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
"Poohface" wrote:
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

On Sat, 23 Apr 2005 07:13:01 -0700, "Xero" <jeff_at_chezjeff_dot_net>
wrote:
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.


Nov 21 '05 #6
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

On Mon, 25 Apr 2005 04:31:04 -0700, "Xero" <jeff_at_chezjeff_dot_net>
wrote:
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
"Poohface" wrote:
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

On Sat, 23 Apr 2005 07:13:01 -0700, "Xero" <jeff_at_chezjeff_dot_net>
wrote:
>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.


Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Adonai | last post: by
3 posts views Thread by Chris Hayes | last post: by
1 post views Thread by BobAchgill | last post: by
2 posts views Thread by Andreas Hohn | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.