VC++中的SetTimer [英] SetTimer in VC++

查看:34
本文介绍了VC++中的SetTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 VC++ 应用程序中创建计时器,在那里我需要设置计时器和计时器超时,我需要调用一个特定的方法...

I need to create Timer in my VC++ application , at where i need to set timer and on timeout of timer , i need to call one specific method...

我已经看到了 msdn 论坛,并在下面的代码中完成了该操作SetTimer(NULL,1,5*1000,TimerProc);

i have seen msdn forums for that and done below code to do that SetTimer(NULL,1,5*1000,TimerProc);

和我的 TimerProc 方法如下

void CALLBACK TimerProc(HWND aHwnd, UINT aMessage, UINT_PTR aTimerId, DWORD aTime)
{
    StopProceess();
}

我假设 5 秒后 SetTimer 应该调用 TimerProc 方法,但它从未被调用.不知道我做错了什么.或者请建议是否有其他选择.

i assume after 5 seconds the SetTimer should call TimerProc method , but it is never called. no idea what i am doing wrong in this. or plz suggest if there's any alternative to do it.

谢谢.

推荐答案

使用可等待计时器.回调将在第 5 个参数设置的时间后调用,并每隔 6 个参数重复一次.时间过去后,回调被调用,执行一些处理并设置事件以允许退出

Use waitable timer. Callback will be called after time set by 5-th parameter and repeat every 6-th parameter. After time elapses, callback is called, executes some processing and sets event to allow exit

    // ConsoleTimer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>

#define TIME_LAPS 5 * 1000

HANDLE g_hExitEvent = NULL;


void CALLBACK WaitOrTimerCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
{
        Sleep(5000); // fake long processing
        SetEvent(g_hExitEvent);
}

int _tmain(int argc, _TCHAR* argv[])
{
        HANDLE hNewTimer = NULL;                                                                                                                        //call after
        BOOL IsCreated = CreateTimerQueueTimer(&hNewTimer, NULL, WaitOrTimerCallback, NULL, TIME_LAPS, 
                // repeat
                TIME_LAPS, WT_EXECUTELONGFUNCTION);

        g_hExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

        WaitForSingleObject(g_hExitEvent, 15000);

        DeleteTimerQueueTimer(NULL, hNewTimer, NULL);

        return 0;
}

我不检查错误,你应该检查.

I do not check for errors, you should.

这篇关于VC++中的SetTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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