Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello,


how to set and reset clock every seconds using MFC/C++.

I want to set clock and open dialog and after 5 seconds the dialog should close and reset the clock.

i am doing like this,
C++
#include <time.h>
clock_t start;
start = clock();
double duration = 0.0;
while(1)
{
 duration = (clock()/ CLOCKS_PER_SEC);
 if(duration == 5)
 {
   //reset the clock here.
 }
}

how to reset the clock now. not with ontimer or settimer.

thanks in advance.
Posted
Updated 12-Feb-13 22:50pm
v3

You need to look into the SetTimer and KillTimer Win32 APIs and the WM_TIMER message. Set a timer on your Dialog window in the OnInitDialog function and when you recieved a WM_TIMER messages use PostMessage to send a message to the dialog to close it. You might want to fake the pressing of an OK or Cancel button rather than sending WM_CLOSE directly, I can't remember off the top of my head but I think Dialogs are a little odd and you have to call a function from within a handler to end one. Anyway do what ever is in the standard on OK or OnCancel handler in your OnTimer function and you'll be fine.
 
Share this answer
 
v2
I think you could get most of what you need from this

http://weseetips.com/2008/08/12/how-to-set-timer-in-mfc-dialog/[^]

define a ID_TIMER_5MINUTE for example as 5 * 60 * 1000

In the OnTimer Event Handler you'd need something like

C#
// Timer Handler.
void CDlgDlg::OnTimer( UINT nIDEvent )
{
    // Per minute timer ticked.
    if( nIDEvent == ID_TIMER_5MINUTE )
    {
        // Do your Five minute based tasks here.
        // Kill The Timer 
        KillTimer( ID_TIMER_5MINUTE  );

        // ToDo : Close the Dialog 
    }

}


Is that the sort of thing you mean need/mean ?
 
Share this answer
 
Comments
Kumar 09 13-Feb-13 4:44am    
No, i am already running the timer in the application. i want to use the clock() functionality to reset the time.(time.h). please find the above improved question.
Garth J Lancaster 13-Feb-13 4:59am    
you cant reset the clock - you can reset your 'start' variable holding a clock interval/reference/value for example - ie, set it to the new start value - start = clock(); .. the other issue is, your calclation for 5 minutes is wrong - wouldnt it look like duration = ((clock() - start)/CLOCKS_PER_SEC) .. that being said, duration is a double, so a straight comparison to 5 as you have isnt going to be useful
Answer to updated question.

Your code should look like this:
C++
clock_t start = clock();
while(1)
{
    if (((clock() - start) / CLOCKS_PER_SEC) >= 5)
        start = clock();
}

However, using such blocking code with Windows GUI applications makes no sense.
 
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