Can't exit application due to an infinite loop in a thread.
Hi. I make a program with Visual C++. First I create, show and update
a Window. Then I launch a thread which has and endless loop. After
that I get, translate and dispatch the messages for my window. When I
close the window, the system distroys it, but the program doesn't
exit. It does only if i remove my loop from the thread. Here is some
code:
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance,LPSTR lpszCmdLine, int nCmdShow){
........./// variables defined
HWND hwndMain = CreateWindow("MainWndClass",
"Sample",WS_OVERLAPPEDWINDOW,100,100,250,210,NULL,NULL, hinst,NULL);
if(!hwndMain)
return FALSE;
ShowWindow(hwndMain,nCmdShow);
UpdateWindow(hwndMain);
_beginthread(myThread, 0,NULL);
while((bRet=GetMessage(&msg,NULL,0,0))!=0){
if (bRet == -1){}
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
void myThread(void *param){
// while(1)
// ;//nothing
//if loop commented everything is OK and program exits, otherwise
windows is closing but program wouldn't exit.
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam,LPARAM
lparam){
if (Message == WM_DESTROY ){
W.stop();
PostQuitMessage(0);
return 0;
}
}
Can you help?