Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinAPI - message loop with own callback

Tags:

c++

c

winapi

The usual WinAPI message loop looks something like this:

MSG msg;
while (GetMessage(&msg, hwnd, 0, 0))
{
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}

Is it allowed not to call DispatchMessage() but to handle the message on your own? If not, how could I nicely approach this behavior while avoiding global variables and thread problems?

Edit: I basically want to use my own callback function, which hasn't the WndProc signature. But I can't think of a way to call that function out of a WndProc without using static or global variables. [Which would require locking, which I think isn't the best thing you can do with a callback function which probably gets called very frequently.]

Thanks for your help.

like image 526
cooky451 Avatar asked May 20 '26 10:05

cooky451


1 Answers

Is it allowed not to call DispatchMessage() but to handle the message on your own? If not, how could I nicely approach this behavior while avoiding global variables and thread problems?

If you are planning to use multiple threads in your GUI then each thread that creates a window will need to manage it's own message queue.

From this page: http://msdn.microsoft.com/en-us/library/ms810439.aspx

Changes to the Message Loop

Applications with multiple threads must include a message loop in each thread that creates a window. The message loop and window procedure for a window must be processed by the thread that created the window. If the message loop does not reside in the same thread that created the window, the DispatchMessage function will not get messages for the window. As a result, the window will appear but won't show activation and won't repaint, be moved, receive mouse messages, or generally work as you expect it to.

like image 167
high5 Avatar answered May 22 '26 23:05

high5