0
101 May 18, 2012 at 09:55

So I decided to try my hand at multi threading today. I thought making a thread to handle window messages would be a simple place to start. However, the window is unresponsive when I try this. Here is the function I am using for my thread:

void DoEvents(void *params)
{
Window* window = (Window*)params;
MSG msg = {0};
while( 1 )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
}


So why would this not work?

9 Replies

0
101 May 18, 2012 at 13:18

How and when do you plan on exiting the loop?

Usually, there’s a:

if (Msg.message == WM_QUIT)
break;


in the PeekMessage() block.

0
101 May 18, 2012 at 14:20

How and when do you plan on exiting the loop?

Usually, there’s a:

if (Msg.message == WM_QUIT)
break;


in the PeekMessage() block.

I planed on using my IDE to break the loop. Any idea why the window is not processing messages?

0
101 May 18, 2012 at 14:48

As far as I remember, you must process messages in the thread where you created target window.

0
175 May 18, 2012 at 15:58

Indeed you shouldn’t separate the message loop from the thread that created your window. All windowing systems I know of adhere to that requirement, including OpenGL contexts. I’ve never had the need to offload the message loop to a separate thread. I keep the message loop in the main application thread and create additional threads as needed when I want to execute logic separate from the main (aka UI) thread.

0
101 May 18, 2012 at 19:04

I realize I did not answer the root question and others did. As has been said by Smile and TheNut, Windows sends messages to the thread that created the window. For additional info, I refer you to thisand this. Of course, there are ways to beat the system into submission, but it is usually so much overhead that it isn’t worth it.

0
101 May 19, 2012 at 01:31

I want to have my library process messages and not have to have the user call a command to do it every frame. What would be the best way to do that? AttachThreadInput? Or is there a way I can do it with the main thread?

0
175 May 19, 2012 at 03:55

0
101 May 19, 2012 at 08:37

@TheNut