user-0 提问: Windows编程在同时2个窗口存在时定时器不起作用

回复:
user: user-1
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-0
content: 引用 1 楼 zhao4zhong1 的回复:两个wndclass使用同一个WndProc，导致SetTimer以同样参数执行了两次。  即使SetTimer一次也不在WndProc里面执行也是一样   用SetTimer(NULL,NULL,1000,ProcTimer)  在WinMain函数 创建一个定时器  窗口过程不创建SetTimer 也不接受WM_TIMER消息  结果也是一样。。。。。。  同样的代码用32位汇编写没问题 换成C 就出现问题了 不知道咋回事

user: user-1
content: 两个wndclass使用同一个WndProc，导致SetTimer以同样参数执行了两次。