Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Metabo

Seperating Message Loop & Game Loop (WIN32)

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So here''s my problem: I''ve tried to seperate the Message Pump from the Game Loop of my engine using Threads. I really need to do this because i''m overriding the WinMain and doing an old-style main() "fake", which would be absolutely useless without seperating the message pump loop from the rest. Even though I''ve searched around this board and the internet and i found a lot of possible solutions i still didn''t get that thing to work... Here''s a bit of the code... This part is taken from the WinMain():
	ATL_Win.hInstance = GetModuleHandle(NULL);

	ATL_Win.hMainLoop = CreateThread(NULL,0,_MainLoop,_main,NULL,&ATL_Win.hMainLoopID); // Create the main loop thread


	
	MSG msg;
	PeekMessage( &msg, NULL, WM_USER, WM_USER, PM_NOREMOVE );
// The message pump loop

	while(1)
	{
		if (PeekMessage( &msg, NULL ,0, 0, PM_REMOVE ))
		{
			if (msg.message == WM_QUIT) 
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else 
		WaitForMessage();

	}
	Terminate = false;

	CloseHandle(ATL_Win.hMainLoop);
	return 0;
}
The MainLoop handle just contains a few window initialization routines and a while-loop. Since i don''t get an error message or a program termination the code seems okay, but it just makes my window freeze und unclickable. ...

Share this post


Link to post
Share on other sites
Advertisement
Closing the thread handle doesn''t stop the thread. Typically, I set some variable to a certain value (I noticed that you set Terminate to false, but I''m not sure what that does), and the in the other thread, it frequently checks that variable, and exits when it equals the correct value. Then, in my other thread that is responsible for causing the side-thread''s termination, I do something like this:
Terminate = true;
if (WaitForSingleObject(hThread, 1000) == WAIT_TIMEOUT)
TerminateThread(hThread, 1);
That way, I try to wait for it to exit correctly, but if it doesn''t exit on its own after 1000 milliseconds, I kill it; I kill it dead. Which doesn''t make Windows very happy, probably, but it shouldn''t happen anyway, and the program does kinda need to exit. Any memory leaks that are caused are minor in a situation like that.

Share this post


Link to post
Share on other sites
Yeah, thats a good idea, thanks. But the main problem is still there. The window doesn''t freeze when i try to close it, it stops to work immediately just after starting the program. I even cannot move it around or try to close it. I''ve played around a bit with the thread and the windows message loop, but i still can''t figure out what i''m doing wrong...

Oh, the false-setting of Terminate was a failure of mine when modifying the code, it actually is set to true there, sorry : )

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!