Hi I'm writing an application that should have when the aplication start working, I'm using the function of C++ "GetTickCount()", the problem is that I read that this function has a 'BUG' that after '49.7' days the function return '0'.
So I'm trying to write a Custom function, I thoug the following solution, to use the 'GetTickCount()' function, rigth after the application start working and save the value in a variable, after that when I will need the function again, I'll check if the 'GetTickCount()' return 0, if yes that means that should use the value saved.
The Problems:
1. When using the function and the function return '0', I'll will loose all the time that pass until the last time
2. The function is use in a lot of places(different cpp files), so in the beggining a I thoug writting a class, but the problem is that every time that I will create the object I start the timer, for the specific object.
How I can do to create like a Global Object, that all the files in the application knows it.

GetTickCount from MSDN:
The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.

From The Web:
There was a load of noise last year about some air traffic control systems implemented on Windows NT (not sure if Embedded or not) which failed after running continuously for 49.7 days. Yup, they'd not allowed for rollover.


Thanks you for your help Thanks
:eek:

Or maybe know the solution of writting my own custom GetTickCount
Thanks

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).

First Thanks, you are rigth, after I posted question, I read that this is not a BUG, is exactly what you write, is a know issue.
I also read that the variable ULONGULONG, is Big enough.
If I undertand your sooution is like this, first I check if this is the first time the application is running (to save the first value), if not the first time, I try to use the GetTickCount() function, if the value is small that the already value, that means we didn't success, we add the current value to the saved value, if not we sucess, Please correct if I'm wrong.

if (bFirsTime)
    {
        u_SavedValue   = GetTickCount();
        bFirsTime = false;
    }
    else
    {
        u_CurrentValue = GetTickCount();

            if( u_CurrentValue < u_SavedValue )
                u_SavedValue += u_CurrentValue;
            else
            u_SavedValue = u_CurrentValue;
    }

Thanks

> 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.

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.

Thanks for your help, I want to write my own GetTickCount that returns ULONGLONG, actually I want to use the GetTickCount64 function, the problem is that this works only in "Windows Vista", so I have to write my own.
The function should return: the value in number of milliseconds that have elapsed since the system was started
Please If someone know, I need your help
Thanks

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;
 }

Thanks, I think this will help, I have only two question:
1. I didn't undertand the line:

fudge = (ULONG_MAX - previous_count) + now;

2. What will happen if I change the variable type from "_int64" to "ULONGLONG", maybe this solve my problem of overflow?
Thanks

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.

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

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

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;

Thanks, for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.