Click here to Skip to main content
11,496,146 members (612 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C++ MFC

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,
#include <time.h>
clock_t start;
start = clock();
double duration = 0.0;
 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 12-Feb-13 23:07pm
Kumar 091.2K
Edited 12-Feb-13 23:50pm
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.
I think you could get most of what you need from this[^]

define a ID_TIMER_5MINUTE for example as 5 * 60 * 1000

In the OnTimer Event Handler you'd need something like

// 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 ?
Kumar 09 at 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 at 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:
clock_t start = clock();
    if (((clock() - start) / CLOCKS_PER_SEC) >= 5)
        start = clock();
However, using such blocking code with Windows GUI applications makes no sense.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100