Sign in to follow this  
Headkaze

Message pump in thread and Win32 window

Recommended Posts

I have an application which creates a window (CreateWindowEx) then just goes into a message loop. I needed to convert this application to a dll, but the message loop stops the dll from exiting halting the application that uses the dll so I placed the message loop in it's own thread.
DWORD WINAPI message_loop(LPVOID lpParam)
{
	MSG msg;

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

	return 1;
}

MYDLL_API initialize()
{
	DWORD dwThreadId= NULL, dwThrdParam = 1;
	int exitcode = 1;
	
	m_hwnd = CreateWindowEx(
	WINDOW_STYLE_EX,
	window_class,
	window_name,
	WINDOW_STYLE,
	0, 0,
	1, 1,
	NULL,
	NULL,
	hInstance,
	NULL);
						
	if (m_hwnd == NULL)
		goto error;

	hThread = CreateThread(NULL, 0, message_loop, &dwThrdParam, 0, &dwThreadId);
	
error:
	return exitcode;
}



Is it okay to have the message loop in a thread of it's own instead of the thread the window is created on? Will the messages be properly dispatched to the window? Should I create the window in the same thread as the message loop? [Edited by - Headkaze on November 7, 2008 12:32:03 AM]

Share this post


Link to post
Share on other sites
No, you can't have the message pump process messages for windows created in other threads and in general doing what you're trying to do is bad practice.

In general, your GUI should be done in one thread, and anything that may block user interaction should move to a separate thread.

Share this post


Link to post
Share on other sites
Thanks for your feedback AAA. I have changed the code to now create the window in the same thread. Can you please verify the following is correct?

DWORD WINAPI create_window(LPVOID lpParam)
{
int exitcode = 1;
WNDCLASS wc = { 0 };

wc.lpszClassName = window_class;
wc.hInstance = hInstance;
wc.lpfnWndProc = window_proc;

if (!RegisterClass(&wc))
goto error;

m_hwnd = CreateWindowEx(
WINDOW_STYLE_EX,
window_class,
window_name,
WINDOW_STYLE,
0, 0,
1, 1,
NULL,
NULL,
hInstance,
NULL);

if (m_hwnd == NULL)
goto error;

MSG msg;

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

DestroyWindow(m_hwnd);
UnregisterClass(window_class, hInstance);

return msg.wParam;

error:
DestroyWindow(m_hwnd);
UnregisterClass(window_class, hInstance);

return exitcode;
}

MYDLL_API initialize()
{
DWORD dwThreadId= NULL, dwThrdParam = 1;

hThread = CreateThread(NULL, 0, create_window, &dwThrdParam, 0, &dwThreadId);
}


MYDLL_API int __stdcall shutdown(void)
{
PostThreadMessage((DWORD) hThread,(UINT) WM_QUIT, 0, 0);

return 1;
}

LRESULT CALLBACK window_proc(HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_CLOSE:
DestroyWindow(wnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(wnd, message, wparam, lparam);
}

return 0;
}

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