galneweinhaw
asked on
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::DisplayTimeElaps ed()
{
...
CString TimeString;
TimeString.Format("%02d:%0 2d",m_Time Elapsed / TT_TicksPerMin, (m_TimeElapsed % TT_TicksPerMin) / TT_TicksPerSec);
...
}
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::DisplayTimeElaps
{
...
CString TimeString;
TimeString.Format("%02d:%0
...
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
THanks, could you explain the difference for me?
ASKER
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!
Still need the OnTimer, but can use this for accurate elapsed time tracking, which is where the problem is right now.
THanks!
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.
ASKER
// Process periodic timer expiry
void CMyClass::OnTimer(UINT nIDEvent)
{
...
if(--m_TimeCarryOverPrecis
{
m_TimeRemain++;
m_PeriodCount++;
m_TimeCarryOverPrecise = 89;
m_TimeElapsed--;
}
if(--m_TimeCarryOver == 0)
{
m_TimeRemain--;
m_PeriodCount--;
m_TimeCarryOver = 5;
m_TimeElapsed++;
}
...
}