|
-
Jul 30th, 2001, 12:04 AM
#1
Count how long app is running
From startup to end, I just want a label showing how long the application has been running, and VB is being a pain in the butt for me, keeps giving me an "overflow".
Basically, this is what I want:
00:00:00 <-0 seconds
00:00:01 <-1 second
00:00:02 <-2 seconds
.....
.....
.....
00:01:00 <-1 minute
00:02:00 <-2 minutes
00:03:00 <-3 minutes
00:04:00 <-4 minutes
00:05:00 <-5 minutes
....
....
............
01:00:00 <-1 hour
This is the code I have, somehow, I believe it's wrong, but maybe someone can help and point me in the right direction. Never had problems like this before.
Code:
Private Declare Function GetTickCount& Lib "kernel32" ()
Label1.Caption = GetTickCount& / (1000 * 60 * 60) Mod 24 & ":" & GetTickCount& / (1000 * 60) Mod 60 & ":" & GetTickCount& / (1000) Mod 60
-
Jul 30th, 2001, 12:14 AM
#2
PowerPoster
Matt:
Have a look through this guys code!!
Found it on PSC!
-
Jul 30th, 2001, 12:39 AM
#3
Fanatic Member
Matthew, it's probably the 1000*60*60 and/or the 1000*60 (either is enough to trigger an overflow). VB's just funky about treating those as integers, and 3600000 and 60000 both exceed that range. Just tack on a & to the 1000 and 60, and it should work fine. And you should save the initial GetTickCount reading when the app first starts too, and subtract that from the current GetTickCount to get the correct time (unless the app starts with windows). GTC by itself is the system uptime. I'm sure you probably knew that, but just thought I'd mention it to be sure.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Jul 30th, 2001, 12:49 AM
#4
-= B u g S l a y e r =-
this is strange ....
I tried this
MsgBox 1000 * 60 * 60
i returns overflow as well.. so I guess u cannot use the * operator on anything that will return a number larger than integer. ????
try replacing it with 3600000
I don't have a good expl. though... sorry
-
Jul 30th, 2001, 01:04 AM
#5
Thanks for the help guys.
Guess I'll just have to use the code from PSC.
-
Jul 30th, 2001, 01:06 AM
#6
Fanatic Member
Well that is the explanation peet. If any whole number expression involving parts that are under the size of an int are used, VB will treat them as ints, and try to force the result into an int as well. Putting type declaration chars on values you suspect may go over the boundaries will prevent it though.
VB Code:
'in debug window
?6000*10 'error
?6000&*10&
60000
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Jul 30th, 2001, 01:33 AM
#7
Registered User
Originally posted by peet
this is strange ....
I tried this
MsgBox 1000 * 60 * 60
i returns overflow as well.. so I guess u cannot use the * operator on anything that will return a number larger than integer. ????
try replacing it with 3600000
I don't have a good expl. though... sorry
Peet
Kaverin was along the right track. Vb appears look at numbers involved in mathematical operations and it makes assumptions about the data types if not explicitly declared beforehand. However it does not always treat undeclared datatype numbers as integers.
E.g. 1
Debug.Print 32767 * 1 will work
Debug.Print 32767 + 1 will fail as limit of integer is reached
here Vb has assumed 32767 and 1 are both integers and tries to hold the result in an integer the limit of which is 32767; so 32767 + 1 creates an overflow error.
E.g. 2
Debug.Print 2147483647 * 1 will work.
Debug.Print 2147483647 + 1 fails as VB tries to hold the result as a long and 2147483647 + 1 breaches the limit of a long
E.g. 3
Debug.Print 1000 * 60 * 60& will fail as VB tries to hold the value of 1000 * 60 in an integer which causes an overflow.
To prevent errors you can declare the datatypes before hand or during the operation with the shorthand declarations such as #,&,%,@.
-
Jul 30th, 2001, 01:56 AM
#8
-= B u g S l a y e r =-
Kaverin and Nucleus
Thanks for giving the explanation to this problem.
I really hate the fact that VB makes assumptions like this. Is it possible to turn this form or assumption off ? Like Option Explicit ?
Or do we just have to live with it. (I'm positive some of my work will crasch somehwere down the road because of this behavior.. )
-
Jul 30th, 2001, 01:58 AM
#9
Fanatic Member
That was a little oversight on my part. I forgot to mention longs in the case that the number already exceeded int. They're still integers though . For floating point values, it seems to default to double though, even if a single can handle it.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Jul 30th, 2001, 02:24 AM
#10
Registered User
Also beware of:
VB Code:
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim r As Long
i = 10000
j = 10000
k = 10
r = i * j * k
MsgBox r
End Sub
Here you need to do this:
VB Code:
Private Sub Command1_Click()
Dim i [b]As Long[/b]
Dim j As Integer
Dim k As Integer
Dim r As Long
i = 10000
j = 10000
k = 10
r = i * j * k
MsgBox r
End Sub
By declaring the first variable i as a long, Vb holds the result in a long all the way through the operation.
Equivalently you would only need to do this in the debug window:
? 10000& * 10000 * 10
In this case only need to declare the first variable as a long and the intermediate results are all held in a long.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|