Results 1 to 10 of 10

Thread: Count how long app is running

  1. #1
    Matthew Gates
    Guest

    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

  2. #2
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Matt:

    Have a look through this guys code!!
    Found it on PSC!

  3. #3
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    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
    -= a peet post =-

  5. #5
    Matthew Gates
    Guest
    Thanks for the help guys.
    Guess I'll just have to use the code from PSC.

  6. #6
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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:
    1. 'in debug window
    2. ?6000*10 'error
    3. ?6000&*10&
    4.  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

  7. #7
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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 #,&,%,@.

  8. #8
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    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.. )
    -= a peet post =-

  9. #9
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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

  10. #10
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Also beware of:

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i As Integer
    3. Dim j As Integer
    4. Dim k As Integer
    5. Dim r As Long
    6.  
    7. i = 10000
    8. j = 10000
    9. k = 10
    10. r = i * j * k
    11. MsgBox r
    12. End Sub

    Here you need to do this:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i [b]As Long[/b]
    3. Dim j As Integer
    4. Dim k As Integer
    5. Dim r As Long
    6.  
    7. i = 10000
    8. j = 10000
    9. k = 10
    10. r = i * j * k
    11. MsgBox r
    12. 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
  •  



Click Here to Expand Forum to Full Width