944,087 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5587
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 5th, 2006
0

Elapse Time Since Program Start Working

Expand Post »
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:
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Omher is offline Offline
13 posts
since May 2006
Jun 5th, 2006
0

Re: Elapse Time Since Program Start Working

Or maybe know the solution of writting my own custom GetTickCount
Thanks
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Omher is offline Offline
13 posts
since May 2006
Jun 5th, 2006
0

Re: Elapse Time Since Program Start Working

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).
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Jun 5th, 2006
0

Re: Elapse Time Since Program Start Working

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.
C++ Syntax (Toggle Plain Text)
  1. if (bFirsTime)
  2. {
  3. u_SavedValue = GetTickCount();
  4. bFirsTime = false;
  5. }
  6. else
  7. {
  8. u_CurrentValue = GetTickCount();
  9.  
  10. if( u_CurrentValue < u_SavedValue )
  11. u_SavedValue += u_CurrentValue;
  12. else
  13. u_SavedValue = u_CurrentValue;
  14. }
Thanks
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Omher is offline Offline
13 posts
since May 2006
Jun 5th, 2006
0

Re: Elapse Time Since Program Start Working

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 5th, 2006
0

Re: Elapse Time Since Program Start Working

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Jun 5th, 2006
0

Re: Elapse Time Since Program Start Working

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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Omher is offline Offline
13 posts
since May 2006
Jun 5th, 2006
0

Re: Elapse Time Since Program Start Working

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)
  1. #include <windows.h>
  2. #include <limits.h>
  3.  
  4. _int64 GetMyTickCount()
  5. {
  6. static _int64 previous_count = 0;
  7. static _int64 fudge = 0;
  8.  
  9. _int64 now = GetTickCount();
  10. if(now < previous_count)
  11. {
  12. fudge = (ULONG_MAX - previous_count) + now;
  13. }
  14. previous_count = now;
  15. return now + fudge;
  16. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Jun 6th, 2006
0

Re: Elapse Time Since Program Start Working

Thanks, I think this will help, I have only two question:
1. I didn't undertand the line:
C++ Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Omher is offline Offline
13 posts
since May 2006
Jun 6th, 2006
0

Re: Elapse Time Since Program Start Working

Quote 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)
  1. 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.
C++ Syntax (Toggle Plain Text)
  1. fudge = ULONG_MAX;


Quote originally posted by Omher ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: IVR system using C++
Next Thread in C++ Forum Timeline: Find if user hit arrow keys





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC