Problem with Timer...(out of sync with reality)

A timer I am useing is totally whacked. Over 36 min, it only counted 16! Computer is oooold and very slow =(  

How do I make this work?

Here is how the timer is currently set-up:

static const int TT_TimeDelay = 500;            
static const int TT_TicksPerSec = (1000) / TT_TimeDelay;   // Timer ticks per second                        
static const int TT_TicksPerMin = (60000)/ TT_TimeDelay;   // Timer ticks per minute

BOOL CMyClass::StartTimer()
{
...
SetTimer(1, (int)TT_TimeDelay, NULL)
...
}

// Process periodic timer expiry
void CMyClass::OnTimer(UINT nIDEvent)
{
...
m_TimeElapsed++;
...
}


void CMyClass::DisplayTimeElapsed()
{
...
CString TimeString;
TimeString.Format("%02d:%02d",m_TimeElapsed / TT_TicksPerMin, (m_TimeElapsed % TT_TicksPerMin) / TT_TicksPerSec);
...
}
galneweinhawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

galneweinhawAuthor Commented:
Hmmm... there is also this, which looks like it is supposed to be correcting time problems....


// Process periodic timer expiry
void CMyClass::OnTimer(UINT nIDEvent)
{
...
   if(--m_TimeCarryOverPrecise == 0)
   {
      m_TimeRemain++;
      m_PeriodCount++;
      m_TimeCarryOverPrecise = 89;
      m_TimeElapsed--;
   }
   if(--m_TimeCarryOver == 0)
   {
      m_TimeRemain--;
      m_PeriodCount--;
      m_TimeCarryOver = 5;
      m_TimeElapsed++;
   }
...
}
0
jkrCommented:
If you only want to measure the elapsed time, a timer (as a limited resource) is not the best choice. You could just use

BOOL CMyClass::StartTimer()
{
...
m_dwStartTime = GetTickCount();
...
}

void CMyClass::DisplayTimeElapsed()
{
...
DWORD dwNoe = GetTickCount();
CString TimeString;
TimeString.Format("%d seconds",(dwNow - m_dwStartTime) / 1000);
...
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
galneweinhawAuthor Commented:
THanks, could you explain the difference for me?
0
galneweinhawAuthor Commented:
Ah... duh.  I see it.  Thanks alot that will work perfect =)

Still need the OnTimer, but can use this for accurate elapsed time tracking, which is where the problem is right now.

THanks!
0
jkrCommented:
A timer will call a message handler in your app that will allow you to increase your internal time value, which requires the process of sending and receiving messages. 'GetTickCount()' retrieves the number of milliseconds that have elapsed since the system was started. This is provided by the system, and the extra cost is just one single function call. So, to get the elapsed time, you call it at the start time, store the value, continue and call it again when the measurement period is over. I.e. if you run your code for 36 mins, your timer would have processed 72 messages, whereas using the other method, all the overhead added are just two funciton calls.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.