We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

galneweinhaw
galneweinhaw asked
on
Medium Priority
248 Views
Last Modified: 2013-11-20
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);
...
}
Comment
Watch Question

Author

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++;
   }
...
}
CERTIFIED EXPERT
Top Expert 2012
Commented:
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);
...
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
THanks, could you explain the difference for me?

Author

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!
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.