there is no bug in GetTickCount(). The reason it wraps around is that a DWORD can only hold a finite number of milliseconds and it reaches its maximum at 49.7 days. So if your program runs longer than that without being stopped and restarted, then you will have to compensate for that feature
Checking to find out if GetTickCount() returns 0 is very unlikly to occur -- you need to check if the most recent value is less than some previous value.
You might consider saving the vales in a 64-bit integer, which will give you a much longer time period. when the value returned by GetTickCount() rolls over you can easily calculate the new value (add new value to previous value and store in a 64-bit number).
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
> Elapse Time Since Program Start Working
What do you mean?
The amount of time, as measured by the clock on the wall?
The amount of time the program has used?
If your program is low priority, say it's only using 10% of the CPU, there is a big difference between "user" time and "program" time.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
ULONGULONG is a 64bit integer. Yes its bigger than DWORD (unsigned long) but it will have the same identical problem if your program runs long enough without stopping.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
something like this?? But of course this won't work either when 64bit integer get data overflow, and assumes this function will get called more frequently than once or twice every 49 days!
#include <windows.h>
#include <limits.h>
_int64 GetMyTickCount()
{
static _int64 previous_count = 0;
static _int64 fudge = 0;
_int64 now = GetTickCount();
if(now < previous_count)
{
fudge = (ULONG_MAX - previous_count) + now;
}
previous_count = now;
return now + fudge;
}
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Thanks, I think this will help, I have only two question:
1. I didn't undertand the line:
fudge = (ULONG_MAX - previous_count) + now;
The more I think about this the more I think the value only needs to be ULONG_MAX. After roll-over only need to add ULONG_MAX to get the 64bit tick count.
fudge = ULONG_MAX;
2. What will happen if I change the variable type from "_int64" to "ULONGLONG"
Nothing. ULONGLONG is just a typedef for _int64 except _int64 is signed.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
fudge = ULONG_MAX;
Let me undertad this, that after we roll-over, we assign the value to the variable?
Is this mean that we set the variable "fudge" with the maximum value that can use?
What I didn't undertand is:
After we roll over that means that we don't have the current value of the GetTickCount() function, how we can rescue the current value, if the function returns '0', how I can get the current value?
Thank, for you patience
dev.cplusplus
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
I undertand my question, and I think the solution is to add the previous_count variable we have storage + the ULONG_MAX, this way we receive, the maximum of the DWORD value + the previous_count, please correct me if I'm wrong
fudge = ULONG_MAX + previous_count;
Thanks
dev.cplusplus
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
when the DWORD rolls over, we don't really care what the previous count was -- just need to add ULONG_MAX to current return from GetTickCount() to get the 64bit tick count. So the value of fudge after roll-over should be just ULONG_MAX. Example: suppose ULONG_MAX == 10, previous value was 9 and current value is 1. Then the return value from that function should be ULONG_MAX + 1 == 11. When it rolls over the second time, the return value is (ULONG_MAX*2) + new value or (10 * 2) = 20 + new value. This continues until fudge variable rolls-over, at which time the function stops working and results become unpredictable.
fudge += ULONG_MAX;
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
dev.cplusplus
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0