-
Aug 4th, 2005, 07:29 AM
#1
Thread Starter
Member
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 07:38 AM.
-
Aug 4th, 2005, 07:35 AM
#2
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:
Private Declare Function GetTickCount Lib "kernel32" ( _
) As Long
' --------------------
Dim lStartTime As Long
Dim lEndTime As Long
lStartTime = GetTickCount()
' Do something...
lEndTime = GetTickCount()
MsgBox "That took a whopping " & (lEndTime - lStartTime) & " ms!"
-
Aug 4th, 2005, 07:55 AM
#3
Thread Starter
Member
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.
-
Aug 4th, 2005, 07:58 AM
#4
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.
-
Aug 4th, 2005, 10:30 AM
#5
Fanatic Member
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:
Dim startTime, endTime, delayTime, elapsedTime As LARGE_INTEGER
QueryPerformanceCounter endTime
QueryPerformanceCounter startTime
delayTime = startTime - endTime
QueryPerformanceCounter startTime
' run your code here
QueryPerformanceCounter endTime
elapsedTime = endTime - startTime - (delayTime * 2)
-
Aug 4th, 2005, 10:36 AM
#6
Re: Can we use 'TimeSpan' in VB6?
Originally Posted by anotherVBnewbie
VB Code:
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:
Dim startTime As Currency
Dim endTime As Currency
Dim delayTime As Currency
Dim elapsedTime As Long
QueryPerformanceCounter endTime
QueryPerformanceCounter startTime
delayTime = startTime - endTime
QueryPerformanceCounter startTime
' run your code here
QueryPerformanceCounter endTime
elapsedTime = (endTime - startTime - (delayTime * 2)) * 10000
Last edited by penagate; Aug 4th, 2005 at 10:39 AM.
-
Aug 4th, 2005, 10:45 AM
#7
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|