Sign in to follow this  
jdub

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

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this