(C#) Why does a simple PeekMessage() lock up my computer

Started by
3 comments, last by yewbie 15 years, 5 months ago
I have this code:

Message msg;

            while (stillIdle)
            {
                //see if it is time to render
                if (gameTime.EllapsedTime >= (1000 / 60) || gameTime.IsFirstUpdate)
                {
                    Render3DEnviroment ( gameTime.EllapsedTime, gameTime.TotalTime );
                    UpdateGame ( gameTime.EllapsedTime, gameTime.TotalTime );
                }
            }
            while( PeekMessage(out msg, gameForm.Handle, 0, 0, 0) )
            {
                switch ((WM)msg.Msg)
                { 
                    case WM.MOUSEMOVE:
                        Application.Exit ();
                        break;
                }
            }

Every time I run it, the program freezes up. The window is cleared red as it should be but when I ran it in the debugger and set some breakpoints, it showed that the code was only hitting the while(PeekMessage(...)) loop. Why could this be? Could I be infinitely generating messages in this code? Thanks, jdub
J.W.
Advertisement
Where are you getting this PeekMessage function from? Is a P/Invoke to the native PeekMessage?

There's three obvious problems:

1. You're calling PeekMessage, but you're not doing anything with those messages (i.e. you're not calling TranslateMessage/DispatchMessage.)
2. You're passing 0 for the (presumably) wRemove parameter which means the message is not being removed from the queue -- you're just peeking the same message over and over.
3. You're passing in a window handle which means thread messages and messages for other windows will not be processed.

The solutions to these problems all stem from my first question: where did this PeekMessage function come from?
It's also not clear what actually sets stillIdle to false. Are you sure you even get to the PeekMessage call? Although everything Codeka points out is true as well.
dont know whats really the problem, but why dont you do it that way:

while(msg.message != WM_QUIT){				if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))		{		// If there are Window messages then process them.		//translate/dispatch           	}		else           	{			// animate	           	}}


works fine here
Do you actually have a windows message proc setup? I think you still need one outside of peek message.

This topic is closed to new replies.

Advertisement