unsigned WINAPI GameThread(LPVOID arg1) { // Run the window's game loop OpenGLWindow.RunGame(); // The thread is done, alert the message handler PostMessage(NULL,WM_CLOSE,0,0); // Alert the main thread that this thread is done GameThreadDoneCS.On(); // Ensuring we're the only ones writing right now GameThreadDone = true; GameThreadDoneCS.Off(); // Protection is over return 0;}// Main windows functionint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; // For processing messages bool done; // For the main loop // Get the screen resolution to run in if (!OpenGLWindow.ChooseScreenRes(hInstance)) return 0; // Didn't click the OK button // Loop around unless we aren't toggling fullscreen mode do { GameThreadDoneCS.On(); // I think I'm getting carried away with these, but I'm not taking many chances GameThreadDone = false; GameThreadDoneCS.Off(); // Protection off // Create the OpenGL window (but not everything) if (!OpenGLWindow.CreateWnd(ProgramName, CustomGLWndProc)) { MessageBox(NULL,"Unable to create new window (in main)","ERROR",MB_OK|MB_ICONEXCLAMATION); return 0; // Problem creating the window } done = false; // Main message pump loop while (!done) { // Get any message that may be waiting if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // Is it a quit message? if (msg.message == WM_QUIT) { // Program is over done = true; } else { // Process the message TranslateMessage(&msg); DispatchMessage(&msg); } } // Check to see if the thread is done yet so we can exit if ((GameThreadDone == true) && (ToggleFullscreen == true)) { done = true; } else if (GameThreadDone == true) { PostQuitMessage(0); } if (!bProgramActive) WaitMessage(); // Releases control until another message comes } OpenGLWindow.DestroyWnd(); } while (ToggleFullscreen == true); return int(msg.wParam); // Exit code}
This is the way that works. I really don't want to terminate the thread by any means other than returning, so I'm keeping away from TerminateThread (especially since THIS works).
All I'm doing to test the GetMessage is converting PeekMessage to GetMessage (and removing PM_REMOVE, of course) and then taking out the WaitMessage stuff.
I've tried moving around the order I tell the main loop my thread is done (post the message afterwards) but it will still hang at the GetMessage. At least, I'm assuming that's where it's hanging because that's the statement the debugger is always on when I pause execution.
Edited by - Dragonskin on December 20, 2001 4:51:40 PM