关于SetTimer,调用函数不执行
interTime=10;我定义了一个时钟:SetTimer(NULL,NULL,interTime,MyTimerProc)MyTimerProc函数如下:VOIDCA...
interTime = 10;
我定义了一个时钟 :SetTimer(NULL,NULL, interTime, MyTimerProc)
MyTimerProc函数如下:VOID CALLBACK MyTimerProc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
)
{
cerr <<"rabbit , I love you!\n"<<endl;
AfxMessageBox("rabbit");
}
时钟设置正确,但有时候会定时执行函数,有时候不会?
为什么?
完全代码如下:
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
//#include <Afxcoll.h>
#include <windows.h>
#include <winuser.h>
//#include <StdAfx.h>
VOID CALLBACK myFunc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
)
{
cerr <<"rabbit , I love you!\n"<<endl;
// printf("rabbit , I love you!\n");
// AfxMessageBox("rabbit , I love you!");
}
int main()
{
UINT interTime = 100;
cerr<<SetTimer(NULL,NULL, interTime,(TIMERPROC)myFunc)<<endl;
Sleep(100000);
return 0;
} 展开
我定义了一个时钟 :SetTimer(NULL,NULL, interTime, MyTimerProc)
MyTimerProc函数如下:VOID CALLBACK MyTimerProc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
)
{
cerr <<"rabbit , I love you!\n"<<endl;
AfxMessageBox("rabbit");
}
时钟设置正确,但有时候会定时执行函数,有时候不会?
为什么?
完全代码如下:
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
//#include <Afxcoll.h>
#include <windows.h>
#include <winuser.h>
//#include <StdAfx.h>
VOID CALLBACK myFunc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
)
{
cerr <<"rabbit , I love you!\n"<<endl;
// printf("rabbit , I love you!\n");
// AfxMessageBox("rabbit , I love you!");
}
int main()
{
UINT interTime = 100;
cerr<<SetTimer(NULL,NULL, interTime,(TIMERPROC)myFunc)<<endl;
Sleep(100000);
return 0;
} 展开
展开全部
你好,关于你的问题,我也不太清楚原因,所以我也尝试着去写了一个程序验证一下,结果并没有出现你所说的情况。代码几乎跟你一样。而对于gww1bd 的回答,我则不同意,MSDN里有讲到如果第一个参数是空的,则第二个参数会被忽略。
而我又看到这么一段话:When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER.
你可以尝试着去调用LRESULT DispatchMessage(
CONST MSG *lpmsg // message information
);
If the lpmsg parameter points to a WM_TIMER message and the lParam parameter of the WM_TIMER message is not NULL, lParam points to a function that is called instead of the window procedure.
你先试试 ,不行的话再联系我。
而我又看到这么一段话:When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER.
你可以尝试着去调用LRESULT DispatchMessage(
CONST MSG *lpmsg // message information
);
If the lpmsg parameter points to a WM_TIMER message and the lParam parameter of the WM_TIMER message is not NULL, lParam points to a function that is called instead of the window procedure.
你先试试 ,不行的话再联系我。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询