Timer Application

I am trying to make an application to time my working hours. Eg. - an app that sits in the tray, and when I tell it to start, it counts how many hours:minutes:seconds. My main poblem, is that it is never accurate enough, and the doevents function stops the timer. Anyone got some good source?
bubblesAsked:
Who is Participating?
 
bear454Connect With a Mentor Commented:
I am helping someone with a similar problem - the solution is to use the Timer to update your value, but use the system time as the value. That is, you are not relying on the accuracy of Timer, only using it to tell the app when to update.  As long as it occurs withing any given second, you are accurate.

Here's some code:

The key is to crunch the math of the date data type. left of the decimal is the number of days, to the right is the time. For example 1/24 or ~0.04166666667 is 1 hour, or 1/86400 or ~0.00001157407 is 1 second.

Public StartTime as date
Timer1.interval = 250 'run every quarter-second

Timer1_Timer()
    Label1.Caption = Format(((Now - StartTime) \(1/24)),"00") & _
      ":" & Format(((Now - Starttime) \ (1/1440)),"00" & _
      ":" & Fomtat(((Now-Starttime) \(1/86400)),"00"
End Sub

For a little more info, check this question:
experts-exchange.com/topics/comp/lang/visualbasic/Q.10114745
0
 
tlw2651Commented:
A suggestion:  Instead of trying to use the timer write a piece of code to have your app store the results of TIME = NOW() in a database table with one Start column and one End column.  Then all you have to do is subtract one from the other...
0
 
bubblesAuthor Commented:
That's sort of what I want to do. I havn't actually been using the timer control. I want some code though (that works).
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
trilloCommented:
The easiest way is the following: You define two variables of type "Date". When you start, you assign the current date and time to the first variable using the "now" function, and you do the same with the second variable at the end of the desired time.
You can calculate the difference with the DateDiff function.
I just tried the following example in VB 4 and it worked great.

-----------Begin code----------
Dim StartTime As Date
Dim StopTime As Date

Private Sub Start_Click()
    StartTime = Now
End Sub

Private Sub Stop_Click()
    StopTime = Now
    s = DateDiff("s", StartTime, StopTime)
    Me.Print s   'print elapsed time in seconds (see note below)
End Sub
-----------End code----------

The first parameter of the "DateDiff" function is the interval, this will determine in which units will the time (or date) difference be returned.

interval      Time interval
yyyy      Year
q      Quarter
m      Month
y      Day of year
d      Day
w      Weekday
ww      Week
h      Hour
n      Minute
s      Second

The other advantage is that you don't waste time: first with the timer, and second with the DoEvents function.

The version with a Timer control would be something like this.
Dim seconds As Long

-----------Begin code----------
Private Sub Start_Click()
seconds = 0
Timer1.Interval = 1000
End Sub

Private Sub Stop_Click()
Timer1.Interval = 0
Me.Print seconds
End Sub

Private Sub Timer1_Timer()
seconds = seconds + 1
End Sub
-----------End code----------

Hope this helped
Trillo
0
 
bubblesAuthor Commented:
Your answer is not suitable. Using a timer control is not accurate enough, and the other mothod doesn't allow for a timer that updates each second. Sorry
0
 
trilloCommented:
What do you mean, It's not accurate enough?... It's enough accurate for your purpose... any of the 2 possibilities.
1) They're accurate up to 50 miliseconds... Or do you really need to know your working hours up to one milisecond?
2) If you want to know your working hours, you don't need a timer to update every "n" time.... it's a waste of memory.

I don't understand why you rejected the answer... maybe I did not understand your purpose, so I'll ask you to explain.

Trillo
0
 
bubblesAuthor Commented:
Thanks. That's more like what I'm after
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.