View Full Version : OpenGL goes to sleep

04-21-2011, 05:17 AM
Hi Everyon,

I am running a piece of openGL code on a windows machine and need to flick between the openGL application and another application as they communicate between each other. When I am using the other application, the openGL code seems to go asleep. I need to see the changes made in the other app in my openGL window in real time, so having to switch application and click on my openGL window is not good.

Is there some option in openGL which you can switch to stop the code going "asleep" and forcing the user to click again on the openGL window?

Any help much appreciated,



04-21-2011, 08:26 AM
Assuming you are using Windows, read the documentation for SetThreadExecutionState (http://msdn.microsoft.com/en-us/library/aa373208(v=vs.85).aspx).

04-21-2011, 11:31 AM
Are you using GetMessage or PeekMessage for your main loop? I suspect the former, as the behaviour of GetMessage is to block until a message is available, but with the app not having focus it will likely not be receiving any messages anyway. Hence the apparent "sleep"ing.

If this is the case then I'd recommend switching to PeekMessage instead. There's a pretty good sample loop here (http://www.mvps.org/directx/articles/writing_the_game_loop.htm). Note that by default this will chew your CPU, so you might want to add a Sleep (5) call to the loop to prevent this if it's not desirable behaviour for your program.

04-21-2011, 10:07 PM
You might also consider using SetTimer() to periodically post WM_TIMER messages to the window message queue. When you get a WM_TIMER message, use InvalidateRect() to tee up a repaint.

This will allow you to block in GetMessage, avoiding CPU burn, but still redraw periodically.

MsgWaitForMultipleObjects() can be used to do more complicated blocks, perhaps combined with CreateWaitableTimer(), waking up either for message input or other events (then you would use PeekMessage to consume all the messages).

04-22-2011, 04:52 AM
WM_TIMER has poor resolution which might lead to jumpy framerates. But depending on the program this might not be an issue; you do need to be aware of it though.

04-23-2011, 05:16 PM
When your OpenGL app receive message from other app, just invalidate opengl window. Use InvalidateRect call to do this.