我可以在控制台 C++ 应用程序中使用 SetTimer() API 吗?

2021-12-31 00:00:00 windows timer console c++

我有一个控制台应用程序,它使用一个 DLL 文件,该文件使用 SetTimer() 调用来创建一个计时器并在其内部触发一个函数.电话如下:

I have a console application that is using a DLL file that uses a SetTimer() call to create a timer and fire a function within itself. The call is below:

SetTimer((HWND)NULL, 0, timer_num, (TIMERPROC)UnSyncMsgTimer)) == 0) 

它期望接收计时器消息,但这从未发生.我假设因为我的是一个控制台应用程序而不是一个标准的 Windows GUI 应用程序(就像最初使用 DLL 文件的地方).这会阻止 DLL 文件功能的关键部分工作.

It is expecting to receive timer messages, but this never happens. I assume because mine is a console application and not a standard Windows GUI application (like where the DLL file was originally used). This stops a key part of the DLL files functionality from working.

我的应用程序需要保持控制台应用程序,我不能更改 DLL.

My application needs to stay a console application, and I cannot change the DLL.

是否有解决方法可以使这项工作正常进行?

Is there a work around to make this work?

推荐答案

您可以使用 CreateTimerQueueTimer 函数

HANDLE timer_handle_;
CreateTimerQueueTimer(&timer_handle_, NULL, TimerProc, user_object_ptr, 10, 0, WT_EXECUTEDEFAULT);
//callback
void TimerProc(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
{
    user_object* mgr = (user_object*) lpParameter;
    mgr->do();
    DeleteTimerQueueTimer(NULL, timer_handle_, NULL);
    timer_handle_ = NULL;
}

相关文章