如何每5秒钟设置和重置一次时钟 [英] how to set and reset clock every 5 seconds

查看:167
本文介绍了如何每5秒钟设置和重置一次时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,





如何使用MFC / C ++每秒设置和重置时钟。



我想设置时钟并打开对话框,5秒钟后对话框应关闭并重置时钟。



i我这样做,

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.
 }
}



现在如何重置时钟。不用ontimer或settimer。



提前谢谢。


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

thanks in advance.

推荐答案

你需要看看 SetTimer KillTimer Win32 API和 WM_TIMER 消息。在OnInitDialog函数的Dialog窗口中设置一个计时器,当你收到WM_TIMER消息时,使用 PostMessage 向对话框发送一条消息以关闭它。您可能想要按下确定或取消按钮,而不是直接发送 WM_CLOSE ,我不记得我的头脑,但我认为Dialogs是一个有点奇怪,你必须从处理程序中调用一个函数来结束一个。无论如何,在你的 OnTimer上确定 OnCancel 处理程序的标准中做什么code>功能,你会没事的。
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.


我想你可以从这里得到你需要的大部分



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



定义ID_TIMER_5MINUTE,例如5 * 60 * 1000



在OnTimer事件处理程序中你需要类似



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 ?


回答更新的问题。



您的代码应如下所示:

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();
}



但是,在Windows GUI应用程序中使用这样的阻止代码毫无意义。


However, using such blocking code with Windows GUI applications makes no sense.


这篇关于如何每5秒钟设置和重置一次时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆