| | |
Elapse Time Since Program Start Working
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 13
Reputation:
Solved Threads: 0
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:
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:
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).
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).
•
•
Join Date: May 2006
Posts: 13
Reputation:
Solved Threads: 0
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.
Thanks
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.
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: May 2006
Posts: 13
Reputation:
Solved Threads: 0
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
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!
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: May 2006
Posts: 13
Reputation:
Solved Threads: 0
Thanks, I think this will help, I have only two question:
1. I didn't undertand the line:
2. What will happen if I change the variable type from "_int64" to "ULONGLONG", maybe this solve my problem of overflow?
Thanks
1. I didn't undertand the line:
C++ Syntax (Toggle Plain Text)
fudge = (ULONG_MAX - previous_count) + now;
Thanks
•
•
•
•
Originally Posted by Omher
Thanks, I think this will help, I have only two question:
1. I didn't undertand the line:
C++ Syntax (Toggle Plain Text)
fudge = (ULONG_MAX - previous_count) + now;
C++ Syntax (Toggle Plain Text)
fudge = ULONG_MAX;
•
•
•
•
Originally Posted by Omher
2. What will happen if I change the variable type from "_int64" to "ULONGLONG"
![]() |
Other Threads in the C++ Forum
- Previous Thread: IVR system using C++
- Next Thread: Find if user hit arrow keys
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






