Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
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,
#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 12-Feb-13 23:07pm
Kumar 091.2K
Edited 12-Feb-13 23:50pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
// 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 ?
  Permalink  
Comments
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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Answer to updated question.
 
Your code should look like this:
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.
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
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