且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-13 07:43:03

您可以使用 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;
}


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) 

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.

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

Is there a work around to make this work?

You can use CreateTimerQueueTimer function

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