我可以在控制台C ++应用程序中使用SetTimer()API吗? [英] Can I use a SetTimer() API in a console C++ application?

查看:341
本文介绍了我可以在控制台C ++应用程序中使用SetTimer()API吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,它使用一个DLL文件,使用 SetTimer()调用创建一个定时器并在自身内部启动一个函数。调用如下:

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

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



我的应用程序需要保留一个控制台应用程序,我不能更改DLL。

$ b

解决方案

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

这篇关于我可以在控制台C ++应用程序中使用SetTimer()API吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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