?
Solved

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

Posted on 2006-03-27
5
Medium Priority
?
233 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);
...
}
0
Comment
Question by:galneweinhaw
  • 3
  • 2
5 Comments
 

Author Comment

by:galneweinhaw
ID: 16303186
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 16303227
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
 

Author Comment

by:galneweinhaw
ID: 16303323
THanks, could you explain the difference for me?
0
 

Author Comment

by:galneweinhaw
ID: 16303365
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
 
LVL 86

Expert Comment

by:jkr
ID: 16303372
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question