Re: why timer not fired in console application? by Roy
Roy
Thu Nov 20 00:37:31 CST 2003
Aling,
You can create timers and implement the timer proc in a console application.
But -- you must implement a message pump to get the timer messages flowing.
It may be easier to to create a message-only window, create the timer for
that window, then implement a trivial message pump. Below, please find a
quick hack that I just threw together to demonstrate the capabilities of a
timer proc in a console app. It prints "hellooooo..." 5 times, then
disappears
That said, it may be easier to start with a straight windows based program
and create a NOT_VISIBLE window - then you don't have the ugly console
window hanging about. If the console window is a required or desired
feature, then disregard the last point.
regards
roy fine
/* ********************************* */
int counter = 5;
HWND hWnd = 0;
/* ********************************* */
void __stdcall MyTimerProc(HWND, UINT, UINT_PTR,DWORD){
printf("\nhellooooo...");
if(--counter < 1) ::PostMessage(hWnd,WM_QUIT,0,0);
}
char *wndClassName = "WNDCLASS_D864FDD1_1D74_4e12_9498_5B3C7F75C55A";
char *wndName = "NONAME";
WNDCLASS wndClass={0,::DefWindowProc,0,0,0,0,0,0,0,wndClassName};
/* ********************************* */
/* ********************************* */
int _tmain(int argc, _TCHAR* argv[]){
::RegisterClass(&wndClass);
hWnd = ::CreateWindow(wndClassName,wndName,0,0,0,0,0,HWND_MESSAGE ,0,0,0);
int id = ::SetTimer(hWnd,1,1000,MyTimerProc);
MSG msg;
while(1){
int sts = ::GetMessage(&msg,hWnd,0,0);
if(sts == 0) break; // handle WM_QUIT case
if(sts < 0) break; // handle Error condition
::DispatchMessage(&msg);
}
return 0;
}
/* end of hack */
"Aling" <alingsjtu@21cn.com> wrote in message
news:emvwDwyrDHA.3456@tk2msftngp13.phx.gbl...
>
> Hi, all,
>
> I created one timer in one console application, and this timer associated
> with one timer-function. This console application also printed some
string
> at console screen.
>
> Now the problem is that the timer funcion never fired though the timer
was
> really created successfully because the timer ID was valid. But if I
> created one separate thread and created the timer within this thread,
then
> all things OK.
>
> Why? We shouldn't create timer in console thread that interacts with
user?
> Any one could answer me? Thanks.
>
> aling
>
> --
>
>