如何在我的 mfc 应用程序的非对话框 .cpp 中使用 SetTimer?
我的问题是针对普通的 mfc SetTimer,如下所示
void CTimersDlg::OnButtonBegin()
{
// create the timer
SetTimer(m_nTimerID, uElapse, NULL);
}
void CTimersDlg::OnButtonStop()
{
// destroy the timer
KillTimer(m_nTimerID);
}
void CTimersDlg::OnTimer(UINT nIDEvent) // called every uElapse milliseconds
{
// do something, but quickly
CDialog::OnTimer(nIDEvent);
}
,但如果我需要在非dialog.cpp 中使用 SetTimer,例如在我的 sender.cpp 中 如何创建计时器?就像在 SetTimer 字段中一样,处理程序(回调)函数?
My question is for normal mfc SetTimer, as follows
void CTimersDlg::OnButtonBegin()
{
// create the timer
SetTimer(m_nTimerID, uElapse, NULL);
}
void CTimersDlg::OnButtonStop()
{
// destroy the timer
KillTimer(m_nTimerID);
}
void CTimersDlg::OnTimer(UINT nIDEvent) // called every uElapse milliseconds
{
// do something, but quickly
CDialog::OnTimer(nIDEvent);
}
but if I need to use SetTimer in non dialog.cpp, for example in my sender.cpp
how do I create the timer? As in the SetTimer fields, the handler(callback) function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以传递 NULL 作为窗口句柄,并在对
SetTimer
的调用中包含回调函数。这将允许您接收计时器通知,而无需将其与特定窗口关联。如果计时器打算在单独的“工作”线程(没有窗口的线程)中使用,您仍然需要处理消息队列才能接收计时器通知。如果您使用
CWinThread
对象创建线程,则在CWinThread::Run
的默认实现中已经为您处理了这一点。如果您可以更新您的问题以包含有关
sender.cpp
内容的更多信息,我可以提供一个更合适的示例。这使用普通的 Windows API 来创建计时器并处理所需的调度队列。You can pass NULL as the window handle and include callback function in the call to
SetTimer
. This will allow you to receive timer notifications without associating it with a specific window.If the timer is intended to be used in a separate "worker" thread (one without a window) you will still need to process the message queue in order to receive timer notifications. If you are creating a thread using a
CWinThread
object this is already handled for you in the default implementation ofCWinThread::Run
.If you can update your question to include more information about the contents of
sender.cpp
I can provide a more suitable example. This uses the plain Windows API to create a timer and handle the required dispatch queue.SetTimer 与窗口关联,需要窗口句柄才能使用它。对于非窗口类,您应该考虑使用 CreateWaitableTimer 或使用 定时器队列
SetTimer is associated with windows, you need windows handle to use it. For non window classes you should consider using CreateWaitableTimer or use TimerQueue