user-0 提问: 定时器不执行 OnTimer方法，要怎么改？

回复:
user: user-0
content: 引用 10 楼 真相重于对错 的回复:查settime的原型啊 感谢大佬，大佬移步，这个帖子随便回复我结贴给分，https://bbs.csdn.net/topics/399174441

user: user-1
content: 查settime的原型啊

user: user-0
content: 引用 7 楼 真相重于对错 的回复:定时器不执行 OnTimer方法，要怎么改？OnTimer现在查到的是只有使用MFC才可以自动调用这个函数，MFC 内部实现了函数映射，但是 我现在不是使用MFC也想用定时器，现在查到的是使用回调函数，现在回调函数可以实现定时器想要的功能了，又出现新问题，我不知道怎么关闭定时器，无论我把KillTimer(NULL,2);这句话写在回调函数第一行还是定时器后面都不执行关闭定时器操作，请问您有什么思路吗？ void CALLBACK TimerProc (   HWND hWnd,   UINT nMsg,   UINT nTimerid,   DWORD dwTime   ){ 	   	//KillTimer(NULL,2); 	keybd_event(VK_RETURN, 0, 0, 0);//按键下去 	keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);//，按键上来，模拟"回车"命令	 		 }; int main(){ 	SetTimer(NULL,2, 1000, TimerProc); 	int ret = MessageBox(NULL,TEXT("0000"),NULL, MB_YESNO); 	KillTimer(NULL,2); 	while(1){ 	//	SetTimer(NULL,2, 1000, TimerProc); 		MessageBox(NULL,TEXT("111111"),NULL, MB_YESNO); 		Sleep(2000); 	} 	 	system("PAUSE"); 	return 0; }

user: user-0
content: 引用 2 楼 赵4老师 的回复:仅供参考：#pragma comment(lib,"user32") #include <stdio.h> #include <time.h> #include <sys/timeb.h> #include <windows.h> CRITICAL_SECTION cs_log; char datestr[16]; char timestr[16]; char mss[4]; void log(char *s) {     struct tm *now;     struct timeb tb;      EnterCriticalSection(&cs_log);     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,s);     LeaveCriticalSection(&cs_log); } VOID CALLBACK myTimerProc1(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc1\n"); } VOID CALLBACK myTimerProc2(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc2\n"); } int main() {     int i;     MSG msg;      InitializeCriticalSection(&cs_log);     SetTimer(NULL,0,1000,myTimerProc1);     SetTimer(NULL,0,2000,myTimerProc2);     for (i=0;i<20;i++) {         Sleep(500);         log("In main\n");         if (GetMessage(&msg,NULL,0,0)) {             TranslateMessage(&msg);             DispatchMessage(&msg);         }      }     DeleteCriticalSection(&cs_log);     return 0; } //2018-04-17 10:07:54.218 In main //2018-04-17 10:07:54.718 In myTimerProc1 //2018-04-17 10:07:55.218 In main //2018-04-17 10:07:55.718 In myTimerProc2 //2018-04-17 10:07:56.218 In main //2018-04-17 10:07:56.218 In myTimerProc1 //2018-04-17 10:07:56.718 In main //2018-04-17 10:07:56.718 In myTimerProc1 //2018-04-17 10:07:57.218 In main //2018-04-17 10:07:57.718 In myTimerProc2 //2018-04-17 10:07:58.218 In main //2018-04-17 10:07:58.218 In myTimerProc1 //2018-04-17 10:07:58.718 In main //2018-04-17 10:07:58.718 In myTimerProc1 //2018-04-17 10:07:59.218 In main //2018-04-17 10:07:59.718 In myTimerProc2 //2018-04-17 10:08:00.218 In main //2018-04-17 10:08:00.218 In myTimerProc1 //2018-04-17 10:08:00.718 In main //2018-04-17 10:08:00.718 In myTimerProc1 //2018-04-17 10:08:01.218 In main //2018-04-17 10:08:01.718 In myTimerProc2 //2018-04-17 10:08:02.218 In main //2018-04-17 10:08:02.218 In myTimerProc1 //2018-04-17 10:08:02.718 In main //2018-04-17 10:08:02.718 In myTimerProc1 //2018-04-17 10:08:03.218 In main //2018-04-17 10:08:03.718 In myTimerProc2 //2018-04-17 10:08:04.218 In main //2018-04-17 10:08:04.218 In myTimerProc1 //2018-04-17 10:08:04.718 In main //2018-04-17 10:08:04.718 In myTimerProc1 //2018-04-17 10:08:05.218 In main //2018-04-17 10:08:05.718 In myTimerProc2 //2018-04-17 10:08:06.218 In main //2018-04-17 10:08:06.218 In myTimerProc1 //2018-04-17 10:08:06.718 In main //2018-04-17 10:08:06.718 In myTimerProc1 //2018-04-17 10:08:07.218 In main //2018-04-17 10:08:07.718 In myTimerProc2   还有个问题需要问一下，  void CALLBACK TimerProc (   HWND hWnd,   UINT nMsg,   UINT nTimerid,   DWORD dwTime   ){ 	   	KillTimer(NULL,2); 	keybd_event(VK_RETURN, 0, 0, 0);//按键下去 	keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);//，按键上来，模拟"回车"命令	 		 }; int main(){  	SetTimer(NULL,2, 1000, TimerProc); 	int ret = MessageBox(NULL,TEXT("111111"),NULL, MB_YESNO); 	system("PAUSE"); 	return 0; } 我这个样子写定时器的关闭，可以实现定时器关闭吗？我不知道要怎么测试，希望得到解答，谢谢啦！  引用 2 楼 赵4老师 的回复:仅供参考：#pragma comment(lib,"user32") #include <stdio.h> #include <time.h> #include <sys/timeb.h> #include <windows.h> CRITICAL_SECTION cs_log; char datestr[16]; char timestr[16]; char mss[4]; void log(char *s) {     struct tm *now;     struct timeb tb;      EnterCriticalSection(&cs_log);     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,s);     LeaveCriticalSection(&cs_log); } VOID CALLBACK myTimerProc1(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc1\n"); } VOID CALLBACK myTimerProc2(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc2\n"); } int main() {     int i;     MSG msg;      InitializeCriticalSection(&cs_log);     SetTimer(NULL,0,1000,myTimerProc1);     SetTimer(NULL,0,2000,myTimerProc2);     for (i=0;i<20;i++) {         Sleep(500);         log("In main\n");         if (GetMessage(&msg,NULL,0,0)) {             TranslateMessage(&msg);             DispatchMessage(&msg);         }      }     DeleteCriticalSection(&cs_log);     return 0; } //2018-04-17 10:07:54.218 In main //2018-04-17 10:07:54.718 In myTimerProc1 //2018-04-17 10:07:55.218 In main //2018-04-17 10:07:55.718 In myTimerProc2 //2018-04-17 10:07:56.218 In main //2018-04-17 10:07:56.218 In myTimerProc1 //2018-04-17 10:07:56.718 In main //2018-04-17 10:07:56.718 In myTimerProc1 //2018-04-17 10:07:57.218 In main //2018-04-17 10:07:57.718 In myTimerProc2 //2018-04-17 10:07:58.218 In main //2018-04-17 10:07:58.218 In myTimerProc1 //2018-04-17 10:07:58.718 In main //2018-04-17 10:07:58.718 In myTimerProc1 //2018-04-17 10:07:59.218 In main //2018-04-17 10:07:59.718 In myTimerProc2 //2018-04-17 10:08:00.218 In main //2018-04-17 10:08:00.218 In myTimerProc1 //2018-04-17 10:08:00.718 In main //2018-04-17 10:08:00.718 In myTimerProc1 //2018-04-17 10:08:01.218 In main //2018-04-17 10:08:01.718 In myTimerProc2 //2018-04-17 10:08:02.218 In main //2018-04-17 10:08:02.218 In myTimerProc1 //2018-04-17 10:08:02.718 In main //2018-04-17 10:08:02.718 In myTimerProc1 //2018-04-17 10:08:03.218 In main //2018-04-17 10:08:03.718 In myTimerProc2 //2018-04-17 10:08:04.218 In main //2018-04-17 10:08:04.218 In myTimerProc1 //2018-04-17 10:08:04.718 In main //2018-04-17 10:08:04.718 In myTimerProc1 //2018-04-17 10:08:05.218 In main //2018-04-17 10:08:05.718 In myTimerProc2 //2018-04-17 10:08:06.218 In main //2018-04-17 10:08:06.218 In myTimerProc1 //2018-04-17 10:08:06.718 In main //2018-04-17 10:08:06.718 In myTimerProc1 //2018-04-17 10:08:07.218 In main //2018-04-17 10:08:07.718 In myTimerProc2  使用回调函数的定时器要怎么关闭呢？

user: user-1
content: 定时器不执行 OnTimer方法，要怎么改？

user: user-2
content: 引用 2 楼 赵4老师 的回复:仅供参考：#pragma comment(lib,"user32") #include <stdio.h> #include <time.h> #include <sys/timeb.h> #include <windows.h> CRITICAL_SECTION cs_log; char datestr[16]; char timestr[16]; char mss[4]; void log(char *s) {     struct tm *now;     struct timeb tb;      EnterCriticalSection(&cs_log);     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,s);     LeaveCriticalSection(&cs_log); } VOID CALLBACK myTimerProc1(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc1\n"); } VOID CALLBACK myTimerProc2(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc2\n"); } int main() {     int i;     MSG msg;      InitializeCriticalSection(&cs_log);     SetTimer(NULL,0,1000,myTimerProc1);     SetTimer(NULL,0,2000,myTimerProc2);     for (i=0;i<20;i++) {         Sleep(500);         log("In main\n");         if (GetMessage(&msg,NULL,0,0)) {             TranslateMessage(&msg);             DispatchMessage(&msg);         }      }     DeleteCriticalSection(&cs_log);     return 0; } //2018-04-17 10:07:54.218 In main //2018-04-17 10:07:54.718 In myTimerProc1 //2018-04-17 10:07:55.218 In main //2018-04-17 10:07:55.718 In myTimerProc2 //2018-04-17 10:07:56.218 In main //2018-04-17 10:07:56.218 In myTimerProc1 //2018-04-17 10:07:56.718 In main //2018-04-17 10:07:56.718 In myTimerProc1 //2018-04-17 10:07:57.218 In main //2018-04-17 10:07:57.718 In myTimerProc2 //2018-04-17 10:07:58.218 In main //2018-04-17 10:07:58.218 In myTimerProc1 //2018-04-17 10:07:58.718 In main //2018-04-17 10:07:58.718 In myTimerProc1 //2018-04-17 10:07:59.218 In main //2018-04-17 10:07:59.718 In myTimerProc2 //2018-04-17 10:08:00.218 In main //2018-04-17 10:08:00.218 In myTimerProc1 //2018-04-17 10:08:00.718 In main //2018-04-17 10:08:00.718 In myTimerProc1 //2018-04-17 10:08:01.218 In main //2018-04-17 10:08:01.718 In myTimerProc2 //2018-04-17 10:08:02.218 In main //2018-04-17 10:08:02.218 In myTimerProc1 //2018-04-17 10:08:02.718 In main //2018-04-17 10:08:02.718 In myTimerProc1 //2018-04-17 10:08:03.218 In main //2018-04-17 10:08:03.718 In myTimerProc2 //2018-04-17 10:08:04.218 In main //2018-04-17 10:08:04.218 In myTimerProc1 //2018-04-17 10:08:04.718 In main //2018-04-17 10:08:04.718 In myTimerProc1 //2018-04-17 10:08:05.218 In main //2018-04-17 10:08:05.718 In myTimerProc2 //2018-04-17 10:08:06.218 In main //2018-04-17 10:08:06.218 In myTimerProc1 //2018-04-17 10:08:06.718 In main //2018-04-17 10:08:06.718 In myTimerProc1 //2018-04-17 10:08:07.218 In main //2018-04-17 10:08:07.718 In myTimerProc2     还有个问题需要问一下，  void CALLBACK TimerProc (   HWND hWnd,   UINT nMsg,   UINT nTimerid,   DWORD dwTime   ){      KillTimer(NULL,2);  keybd_event(VK_RETURN, 0, 0, 0);//按键下去  keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);//，按键上来，模拟"回车"命令     }; int main(){   SetTimer(NULL,2, 1000, TimerProc);  int ret = MessageBox(NULL,TEXT("111111"),NULL, MB_YESNO);  system("PAUSE");  return 0; } 我这个样子写定时器的关闭，可以实现定时器关闭吗？我不知道要怎么测试，希望得到解答，谢谢啦！

user: user-0
content: 引用 2 楼 赵4老师 的回复:仅供参考：#pragma comment(lib,"user32") #include <stdio.h> #include <time.h> #include <sys/timeb.h> #include <windows.h> CRITICAL_SECTION cs_log; char datestr[16]; char timestr[16]; char mss[4]; void log(char *s) {     struct tm *now;     struct timeb tb;      EnterCriticalSection(&cs_log);     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,s);     LeaveCriticalSection(&cs_log); } VOID CALLBACK myTimerProc1(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc1\n"); } VOID CALLBACK myTimerProc2(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc2\n"); } int main() {     int i;     MSG msg;      InitializeCriticalSection(&cs_log);     SetTimer(NULL,0,1000,myTimerProc1);     SetTimer(NULL,0,2000,myTimerProc2);     for (i=0;i<20;i++) {         Sleep(500);         log("In main\n");         if (GetMessage(&msg,NULL,0,0)) {             TranslateMessage(&msg);             DispatchMessage(&msg);         }      }     DeleteCriticalSection(&cs_log);     return 0; } //2018-04-17 10:07:54.218 In main //2018-04-17 10:07:54.718 In myTimerProc1 //2018-04-17 10:07:55.218 In main //2018-04-17 10:07:55.718 In myTimerProc2 //2018-04-17 10:07:56.218 In main //2018-04-17 10:07:56.218 In myTimerProc1 //2018-04-17 10:07:56.718 In main //2018-04-17 10:07:56.718 In myTimerProc1 //2018-04-17 10:07:57.218 In main //2018-04-17 10:07:57.718 In myTimerProc2 //2018-04-17 10:07:58.218 In main //2018-04-17 10:07:58.218 In myTimerProc1 //2018-04-17 10:07:58.718 In main //2018-04-17 10:07:58.718 In myTimerProc1 //2018-04-17 10:07:59.218 In main //2018-04-17 10:07:59.718 In myTimerProc2 //2018-04-17 10:08:00.218 In main //2018-04-17 10:08:00.218 In myTimerProc1 //2018-04-17 10:08:00.718 In main //2018-04-17 10:08:00.718 In myTimerProc1 //2018-04-17 10:08:01.218 In main //2018-04-17 10:08:01.718 In myTimerProc2 //2018-04-17 10:08:02.218 In main //2018-04-17 10:08:02.218 In myTimerProc1 //2018-04-17 10:08:02.718 In main //2018-04-17 10:08:02.718 In myTimerProc1 //2018-04-17 10:08:03.218 In main //2018-04-17 10:08:03.718 In myTimerProc2 //2018-04-17 10:08:04.218 In main //2018-04-17 10:08:04.218 In myTimerProc1 //2018-04-17 10:08:04.718 In main //2018-04-17 10:08:04.718 In myTimerProc1 //2018-04-17 10:08:05.218 In main //2018-04-17 10:08:05.718 In myTimerProc2 //2018-04-17 10:08:06.218 In main //2018-04-17 10:08:06.218 In myTimerProc1 //2018-04-17 10:08:06.718 In main //2018-04-17 10:08:06.718 In myTimerProc1 //2018-04-17 10:08:07.218 In main //2018-04-17 10:08:07.718 In myTimerProc2  谢谢，解决了

user: user-0
content: 引用 1 楼 真相重于对错 的回复:你又没有把定时器和回调函数绑定，系统怎么知道要调用那个函数我知道没绑定，我在查，我没查到才问的！！！！

user: user-3
content: 仅供参考：#pragma comment(lib,"user32") #include <stdio.h> #include <time.h> #include <sys/timeb.h> #include <windows.h> CRITICAL_SECTION cs_log; char datestr[16]; char timestr[16]; char mss[4]; void log(char *s) {     struct tm *now;     struct timeb tb;      EnterCriticalSection(&cs_log);     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,s);     LeaveCriticalSection(&cs_log); } VOID CALLBACK myTimerProc1(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc1\n"); } VOID CALLBACK myTimerProc2(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc2\n"); } int main() {     int i;     MSG msg;      InitializeCriticalSection(&cs_log);     SetTimer(NULL,0,1000,myTimerProc1);     SetTimer(NULL,0,2000,myTimerProc2);     for (i=0;i<20;i++) {         Sleep(500);         log("In main\n");         if (GetMessage(&msg,NULL,0,0)) {             TranslateMessage(&msg);             DispatchMessage(&msg);         }      }     DeleteCriticalSection(&cs_log);     return 0; } //2018-04-17 10:07:54.218 In main //2018-04-17 10:07:54.718 In myTimerProc1 //2018-04-17 10:07:55.218 In main //2018-04-17 10:07:55.718 In myTimerProc2 //2018-04-17 10:07:56.218 In main //2018-04-17 10:07:56.218 In myTimerProc1 //2018-04-17 10:07:56.718 In main //2018-04-17 10:07:56.718 In myTimerProc1 //2018-04-17 10:07:57.218 In main //2018-04-17 10:07:57.718 In myTimerProc2 //2018-04-17 10:07:58.218 In main //2018-04-17 10:07:58.218 In myTimerProc1 //2018-04-17 10:07:58.718 In main //2018-04-17 10:07:58.718 In myTimerProc1 //2018-04-17 10:07:59.218 In main //2018-04-17 10:07:59.718 In myTimerProc2 //2018-04-17 10:08:00.218 In main //2018-04-17 10:08:00.218 In myTimerProc1 //2018-04-17 10:08:00.718 In main //2018-04-17 10:08:00.718 In myTimerProc1 //2018-04-17 10:08:01.218 In main //2018-04-17 10:08:01.718 In myTimerProc2 //2018-04-17 10:08:02.218 In main //2018-04-17 10:08:02.218 In myTimerProc1 //2018-04-17 10:08:02.718 In main //2018-04-17 10:08:02.718 In myTimerProc1 //2018-04-17 10:08:03.218 In main //2018-04-17 10:08:03.718 In myTimerProc2 //2018-04-17 10:08:04.218 In main //2018-04-17 10:08:04.218 In myTimerProc1 //2018-04-17 10:08:04.718 In main //2018-04-17 10:08:04.718 In myTimerProc1 //2018-04-17 10:08:05.218 In main //2018-04-17 10:08:05.718 In myTimerProc2 //2018-04-17 10:08:06.218 In main //2018-04-17 10:08:06.218 In myTimerProc1 //2018-04-17 10:08:06.718 In main //2018-04-17 10:08:06.718 In myTimerProc1 //2018-04-17 10:08:07.218 In main //2018-04-17 10:08:07.718 In myTimerProc2

user: user-1
content: 你又没有把定时器和回调函数绑定，系统怎么知道要调用那个函数