c 中的 TimerProc 不起作用?

问题描述 投票:0回答:3

我正在尝试为我的计时器编写回调。我这样定义 TimerProc:

void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
    //body of callback
}

然后 SetTimer 定义如下:

myTimer = SetTimer(NULL,Timer_ID,30000,TimerProc);

我的问题是,一旦时间过去(30秒),回调就不会被调用。

感谢您的帮助。

c winapi timer callback
3个回答
3
投票

SetTimer
的工作原理是向默认窗口过程发送
WM_TIMER
消息。因此,正如 MSDN 所说:

当您指定TimerProc回调函数时,默认窗口过程在处理WM_TIMER时会调用该回调函数。因此,即使您使用 TimerProc 而不是处理 WM_TIMER,您也需要在调用线程中分派消息。

因此请确保您正在运行消息循环


1
投票

快速测试代码。对我来说效果很好。

#include <windows.h>

static const TCHAR gc_szClassName[]   = TEXT("Test");
static const TCHAR gc_szWindowTitle[] = TEXT("Test");

#define IDT_TIMER 0x100

VOID CALLBACK TimerProc(HWND hWnd, UINT uMessage, UINT_PTR uEventId, DWORD dwTime)
{
  // display a message box to see the results of our beautiful program
  MessageBox(hWnd, TEXT("This should pop up every 10 seconds.."), TEXT("Yay!"), MB_OK | MB_ICONINFORMATION);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
  switch (uMessage)
  {
    case WM_CREATE:
      // run every 10 seconds
      SetTimer(hWnd, IDT_TIMER, 10000, TimerPRoc);
      break;

    case WM_CLOSE:
      DestroyWindow(hWnd);
      break;

    case WM_DESTROY:
      KillTimer(hWnd, IDT_TIMER);
      PostQuitMessage(EXIT_SUCCESS);
      break;
  }

  return DefWindowProc(hWnd, uMessage, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCommandLine, int nShowCommand)
{
  // define variables
  HWND hWnd;
  WNDCLASS wc;
  MSG msg;

  // unused parameters
  UNREFERENCED_PARAMETER(hPrevInstance);
  UNREFERENCED_PARAMETER(lpszCommandLine);
  UNREFERENCED_PARAMETER(nShowCommand);

  // initialize WNDCLASS structure
  ZeroMemory(&wc, sizeof(wc));
  wc.lpfnWndProc = WndProc;
  wc.hInstance = hInstance;
  wc.lpszClassName = gc_szClassName;

  // attempt to register the class
  if (RegisterClass(&wc) != 0)
  {
    // attempt to create the window
    hWnd = CreateWindow(gc_szClassName, gc_szWindowTitle, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL);
    if (hWnd != NULL)
    {
      // retrieve messages
      while (GetMessage(&msg, NULL, 0, 0) > 0)
      {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }

      // use the return-code from the window
      return (int)msg.wParam;
    }
  }
  return EXIT_FAILURE;  
}

0
投票

WM_TIMER不是必需的,只需要消息循环。

#include <windows.h>
#include <iostream>

using namespace std;

VOID CALLBACK Timerproc(
  HWND hwnd,        // handle to window for timer messages 
  UINT message,     // WM_TIMER message 
  UINT idTimer,     // timer identifier 
  DWORD dwTime)     // current system time
{
  cout << "Timerproc" << endl;
}

int main() {
  const UINT_PTR timer_id = SetTimer(nullptr,
                                     0,
                                     0x0,
                                     Timerproc);
  int Counter = 0;
  MSG msg;
  while (GetMessage(&msg, nullptr, 0, 0)) {
    ++Counter;
    cout << "Counter: " << Counter << "; message: " << msg.message << '\n';
    DispatchMessage(&msg);
  }
  return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.