Can we use 'TimeSpan' in VB6?-VBForums
Results 1 to 7 of 7

Thread: Can we use 'TimeSpan' in VB6?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    43

    Can we use 'TimeSpan' in VB6?

    I wish to time a process/task to find out how many milliseconds it takes. Can we use 'TimeSpan' to implement it in VB6? If YES, pls advise how to do it in VB6. If NO, pls advise the function in VB6 that is equivalent to TimeSpan. Thanks.
    Last edited by onemilimeter; Aug 4th, 2005 at 08:38 AM.

  2. #2
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Can we use 'TimeSpan' in VB6?

    I dont know what "TimeSpan" is but heres a general way of timing processes to the nearest millisecond or so:

    VB Code:
    1. Private Declare Function GetTickCount Lib "kernel32" ( _
    2. ) As Long
    3.  
    4. ' --------------------
    5. Dim lStartTime As Long
    6. Dim lEndTime   As Long
    7.  
    8. lStartTime = GetTickCount()
    9.  
    10. ' Do something...
    11.  
    12. lEndTime = GetTickCount()
    13.  
    14. MsgBox "That took a whopping " & (lEndTime - lStartTime) & " ms!"

  3. #3

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    43

    Re: Can we use 'TimeSpan' in VB6?

    Hi penagate:
    GetTickCount determines how much time has elapsed since Windows was last started. The time is measured in milliseconds, although the actual resolution of the function's output depends on that of the system timer itself. Therefore, it may not be perfectly accurate to the millisecond. Because of the limitations of the 32-bit integer data type, the reported elapsed time wraps back to zero after about 49.7 days of continuous operation.
    Without waiting for 49.7 days or restarting the Windows, is it possible to RESET the elapsed time back to zero? Thanks.

  4. #4
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Can we use 'TimeSpan' in VB6?

    I see you do your homework The thing is, since we are measuring the difference between two calls to GetTickCount(), with our process to be timed in between the calls, we get the time elapsed to complete the process. Unless of course you have the unfortunate occurence where you start on one side of the 49.7 day limit and finish on the other, but that's highly unlikely

    If you genuinely meant reset the system uptime however, the answer is no.

  5. #5
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: Can we use 'TimeSpan' in VB6?

    Actually, if you want to be *exact* in timing how long a process takes you should use QueryPerformanceCounter instead of GetTickCount and make 4 calls so that you can also calculate the amount of time the API call required. MS has an example of this on their site somewhere but I think it goes petty much like this:

    VB Code:
    1. Dim startTime, endTime, delayTime, elapsedTime As LARGE_INTEGER
    2.  
    3. QueryPerformanceCounter  endTime
    4. QueryPerformanceCounter startTime
    5. delayTime = startTime - endTime
    6. QueryPerformanceCounter startTime
    7.  
    8. ' run your code here
    9.  
    10. QueryPerformanceCounter  endTime
    11.  
    12. elapsedTime = endTime - startTime - (delayTime * 2)

  6. #6
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Can we use 'TimeSpan' in VB6?

    Quote Originally Posted by anotherVBnewbie
    VB Code:
    1. Dim startTime, endTime, delayTime, elapsedTime As LARGE_INTEGER
    One of the most common mistakes... Only elapsedTime is in fact declared as a LARGE_INTEGER. The rest are Variants.

    Also, for a 64-bit number, it is easier to use the Currency type, and multiply by 10000 to get the result.

    VB Code:
    1. Dim startTime As Currency
    2. Dim endTime As Currency
    3. Dim delayTime As Currency
    4. Dim elapsedTime As Long
    5.  
    6. QueryPerformanceCounter  endTime
    7. QueryPerformanceCounter startTime
    8. delayTime = startTime - endTime
    9. QueryPerformanceCounter startTime
    10.  
    11. ' run your code here
    12.  
    13. QueryPerformanceCounter  endTime
    14.  
    15. elapsedTime = (endTime - startTime - (delayTime * 2)) * 10000
    Last edited by penagate; Aug 4th, 2005 at 11:39 AM.

  7. #7
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: Can we use 'TimeSpan' in VB6?

    Okay, I got to thinking that I had either left something out or was just plain wrong in some of my rememberings, as evidenced by penagate's catching my faulty declares....

    What I was trying to remember how to do can actually be found here. As you'll see, everything should either be currency or 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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.