Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi Guys,

I need to create one timer that should Run when i start my GUI and should Exit when i exit the GUI . the Timer should Start from 00:00:00...pls help me in doing this
Posted
Comments
Jochen Arndt 4-Jul-12 4:13am    
What is your intention? Measuring execution times or performing actions in defined intervals?
Sandeep Mewara 4-Jul-12 4:21am    
Your question does not state where are you stuck in it. Please be specific.
Member 9102753 4-Jul-12 5:19am    
Actually When i start my application the timer should start running ,so that i can find when it has started . i find some examples like CTime curtime=CTime::GetCurrentTime()...but by using that function i can get my pc time while i start my application. here i don't want pc time it should start as a stop watch kind of think
Jochen Arndt 4-Jul-12 5:31am    
Then just store the current time at application start in a member variable and get the elapsed time by subtracting that value from the current time.

Sounds like you want to use the timer in order to measure for how long your application has been running. For that you don't want to create a timer, because timers are used to give you callbacks in regular time intervals. Instead, you want to register the time at the start of your application, then at the end and then calculate the difference between these two values.

Function time lets you easily retrieve a current time value in seconds. So the subtraction of the two time values is also easy to do.

Another option would be to use MFC's CTime and CTimeSpan classes to do the same.

CTime t0 = CTime::GetCurrentTime();
...
CTime t1 = CTime::GetCurrentTime();

CTimeSpan tTotal = t1 - t0;


Hope that gets you up to speed.
 
Share this answer
 
Comments
Jochen Arndt 4-Jul-12 6:04am    
+5 for you after the problem has been brought to light.
nv3 4-Jul-12 6:05am    
Thanks Jochen!
SoMad 9-Jul-12 23:40pm    
Yes, this works as long as you don't change the PC time, time zone or switch to/from daylight saving. Another option is using GetTickCount() and keep track of the 49 day roll-over, but I prefer to use QueryPerformanceCounter() along with QueryPerformanceFrequency().

Soren Madsen
Hi,

Use SetTimer() windows API and overload WM_TIMER message.
Set timer function create a timer for you with given time interval.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900