高手请进!关于定时器的问题! 我在一个对话框程序的OnInitDialog()函数里调用SetTimer()函数没有任何问题,但是在一个SDI应用程序里的构造函数里调用SetTimer()就不行,但是编译能够通过,运行前首先弹出一个assert对话框,忽略后程序可以运行,但是定时器没有启动。这是怎么回事?是不是不该在它的构造函数里调用啊?那么应该在什么地方调用呢?请高手赐教!谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你试试下面的void CMyView::OnInitialUpdate() { SetTimer(1,200,NULL);} //////////////////////////////////////////////////////////////user timer in workthread of console app#include <windows.h>#include <stdio.h>#include <conio.h>unsigned long WINAPI Thread(PVOID pvoid);void main(){ DWORD dwThreadId; printf("use timer in workthread of console application<masterz>\n"); HANDLE hThread = CreateThread( NULL, // no security attributes 0, // use default stack size Thread, // thread function 0, // argument to thread function 0, // use default creation flags &dwThreadId); DWORD dwwait=WaitForSingleObject(hThread,1000*30); switch(dwwait) { case WAIT_ABANDONED: printf("main thread WaitForSingleObject return WAIT_ABANDONED\n"); break; case WAIT_OBJECT_0: printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n"); break; case WAIT_TIMEOUT: printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n"); break; } CloseHandle(hThread); _getch();}unsigned long WINAPI Thread(PVOID pvoid){ MSG msg; PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); UINT timerid=SetTimer(NULL,111,3000,NULL); BOOL bRet; int count =0; while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) { if (bRet == -1) { // handle the error and possibly exit } else if(msg.message==WM_TIMER) { count++; printf("WM_TIMER in work thread count=%d\n",count); if(count>4) break; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } KillTimer(NULL,timerid); printf("thread end here\n"); return 0;} 不能在构造函数中SetTime()!可以在app的initinstance中加入! 不可以在构造函数里调用定时器,因为此时窗口还没有创建。可以在OnCreate()里调用。 你可以在CXXView的OnCreate中来启动定时器 AVi文件只能用暴风影音播放的问题 关于vc2008显示的一个小问题,非常简单 vc调用webservice 请问我在打开vc的对话框时,会提示"the file has been modified outside of the source editor.do you want reload it"? 小弟弱问一个问题 希望高手路过看看 小弟急等啊!!! 请问怎么拖动一个字符串在视图中移动呀? 请教:关于ListCtrl(100分) 关于COM小问题??? 送分给0分的同志,和有必要的同志 请教一个domodal的问题。 哪位大虾帮小弟一把? CTreeCtrl的问题,不知道该 怎么办了
void CMyView::OnInitialUpdate()
{
SetTimer(1,200,NULL);
}
user timer in workthread of console app
#include <windows.h>
#include <stdio.h>
#include <conio.h>
unsigned long WINAPI Thread(PVOID pvoid);
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application<masterz>\n");
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
break;
}
CloseHandle(hThread);
_getch();
}unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
可以在app的initinstance中加入!