English 中文(简体)
VC++ 中的设置时间器
原标题:SetTimer in VC++

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

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

i 假设5秒后,SetTimerr 应该调用 TimerProc 方法,但从未调用过。 不知道我在这个方法中做错了什么。 或 plz 表示, 是否有其他方法可以这样做 。

谢谢

最佳回答

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

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

问题回答

从代码片断我非常肯定你正在写基于控制台的应用程序。

您可以使用其他类型的计时器, 或者如果您不真正关心精确度的话, 您错过了一件事, 以便让这个计时器工作 。

Even though this is a console app, your timer callback will not be called unless you dispatch timer message. Do this:

    MSG msg;
    SetTimer(NULL, 1, 5*1000 ,TimerProc);

    while (GetMessage(&msg, NULL, WM_NULL, WM_TIMER)) 
    { 
        DispatchMessage(&msg); 
        if (WM_TIMER == msg.message) 
        {
            break;
        }
    }
    StopProceess();

考虑使用可等待计时器 。

对我来说这看起来不错。 您是否在您的 Timer Proc 中设置了断点? 您是否在您的程序中设置了断点? 您是否在 SetTimer 中设置了断点? 您需要确定您真的给SetTimer 打电话, 而 Timer Proc 并没有被真正拨打 。

接下来我要问的是,您是否在信息循环中发送信息? 如果您不是在信息循环中发送信息, 我不认为定时器信息永远不会被解开。 如果您在您的程序中执行一些漫长的程序, 而且您永远不会用相应的快递邮件给GetMessage/PeakMessage打电话, 那么这可能导致定时器信息永远不会被解开 。

这 < a href=>" "http://msdn.microsoft.com/en-us/library/windows/desktop/ms644901%28v=vs.85%29.aspx" rel="nofollow" > article 经历了创建和摧毁定时器的过程。

我找到了一些 < a href=> http://en.wikibook.org/wiki/Windows_Programming/Interfacing> rel=“nofolt” >example code ,它仍然使用 hWnd 参数,有什么理由你不使用 hWnd 参数吗?如果您不能使用参数,您可以尝试在您通过 NULL 时查看代码是否有效。我注意到,在示例中,他们将代码回调到 < code> TIEMERPROC 。





相关问题
how to reliable capture display setting changed

static void Main() { // Set the SystemEvents class to receive event notification when a user // when display settings change. SystemEvents.DisplaySettingsChanged += new ...

Why use CComBSTR instead of just passing a WCHAR*?

I m new to COM. What exactly is the advantage of replacing: L"String" with CComBSTR(L"String") I can see a changelist in the COM part of my .NET application where all strings are replaced in this ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Statically linking Winsock?

I m using Winsock 1.1 in my project. I include wsock32.lib in "Additional Dependencies". I m looking at the DLL project using depends.exe and notice that the DLL depends on wsock32.dll. How can I ...

热门标签