473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3291
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
0
by: Adonai | last post by:
Hi, I'm having an issue with this code that I'm writing, I'm curious as to why the windows form label (lblTime) isn't getting updated with the new variable when I use the timer. I made a blank form...
5
by: Dhilip Kumar | last post by:
Hi all, I have developed a windows service using the windows service project template in VS.NET. I have used three controls in the service, a timer, performance counter and a message queue...
3
by: Chris Hayes | last post by:
I'm trying to create a nifty Windows Service that will perform tasks at a predetermined interval. To make sure I understand the timing correctly I have set an emailer utility to email me on the...
5
by: Trint Smith | last post by:
I need to display a timer ticking down. Example: 11h:52m:39s to 00h:00m:00s How can I do this? Thanks, Trint .Net programmer trintsmith@hotmail.com
1
by: BobAchgill | last post by:
Is there a way to kill or stop a timer once it is started? Or is there another better way to preempt a timed process before it times out? I have tried these combinations but timer1 just keeps...
5
by: Flack | last post by:
Hey guys, Here is what I am trying to achieve: I have a grid, and every once in a while the grid will receive a message to add a new row and highlight it (change the backcolor) for five...
8
by: KnighT | last post by:
I have a .net service that runs a System.Threading.Timer. The delegate points to the function that the service should execute when the timer elapses. Problem: The timer is not ticking. I have...
2
by: Andreas Hohn | last post by:
Hi, what is the right way to start a timer? timer.enabled = true or timer.start()?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.