• Advertisement
Sign in to follow this  

problems creating a window from another thread

This topic is 4469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's my (long) problem. I have a class, resource, that's abstract, and I have a DLL with a function CreateResource(resource** result) that inistantiates a derived class and put it into the pointer pointed to by result (*result = new derivedClass();). Resource has a method DoWork() that's as follows:
boost::thread(RealWorkFunction);
return;

RealWorkFunction:
NewWindow();
return;

NewWindow:
WNDCLASS	wc;						// Windows Class Structure
	DWORD		dwExStyle;				// Window Extended Style
	DWORD		dwStyle;				// Window Style
	RECT		WindowRect;				// Grabs Rectangle Upper Left / Lower Right Values
	WindowRect.left=(long)0;			// Set Left Value To 0
	WindowRect.right=(long)Width;		// Set Right Value To Requested Width
	WindowRect.top=(long)0;				// Set Top Value To 0
	WindowRect.bottom=(long)Height;		// Set Bottom Value To Requested Height
	hInstance			= GetModuleHandle(NULL);				// Grab An Instance For Our Window
	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
	wc.lpfnWndProc		= (WNDPROC) WndProc;					// WndProc Handles Messages
	wc.cbClsExtra		= 0;									// No Extra Window Data
	wc.cbWndExtra		= 0;									// No Extra Window Data
	wc.hInstance		= hInstance;							// Set The Instance
	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
	wc.hbrBackground	= NULL;									// No Background Required For GL
	wc.lpszMenuName		= NULL;									// We Don't Want A Menu
	wc.lpszClassName	= "X";								// Set The Class Name

	if (!RegisterClass(&wc))									// Attempt To Register The Window Class
	{
		MessageBoxA(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		//return void;// FALSE;											// Return FALSE
	}



		dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended Style
		dwStyle=WS_OVERLAPPEDWINDOW;							// Windows Style

	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);		// Adjust Window To True Requested Size

	// Create The Window
	if (!(hWnd=CreateWindowExA(	dwExStyle,							// Extended Style For The Window
		"X",							// Class Name
		Title,								// Window Title
		dwStyle |							// Defined Window Style
		WS_CLIPSIBLINGS |					// Required Window Style
		WS_CLIPCHILDREN,					// Required Window Style
		0, 0,								// Window Position
		WindowRect.right-WindowRect.left,	// Calculate Window Width
		WindowRect.bottom-WindowRect.top,	// Calculate Window Height
		NULL,								// No Parent Window
		NULL,								// No Menu
		hInstance,							// Instance
		NULL)))								// Dont Pass Anything To WM_CREATE
	{
		MessageBoxA(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		//return void;// FALSE;								// Return FALSE
	}


	ShowWindow(hWnd,SW_SHOW);						// Show The Window
	SetForegroundWindow(hWnd);						// Slightly Higher Priority
	SetFocus(hWnd);

WndProc:
Log << "windowproc hit." << endl;
return DefWindowProc(...);

now, when I try to do a main like this:
LoadDll();
resource* R;
CreateResource(&R);
R->DoWork();
while(!kbhit())
{
}
R->Destroy();
UnloadDLL();

the window appears and crashes (clicking inside it turns it tielte to non-responding). any Ideas? sorry for the long post and messed up code. TIA.

Share this post


Link to post
Share on other sites
Advertisement
I have no idea whether this is your problem or not, but often libraries--GUIs especially--only allow you to use them from one thread.

Share this post


Link to post
Share on other sites
Where's the message pump? You need to run a PeekMessage/GetMessage, TranslateMessage, DispatchMessage loop in the same thread as the one who called CreateWindow, otherwise your windows will appear to be "hanging".

Share this post


Link to post
Share on other sites
Quote:
Original post by Endurion
Where's the message pump? You need to run a PeekMessage/GetMessage, TranslateMessage, DispatchMessage loop in the same thread as the one who called CreateWindow, otherwise your windows will appear to be "hanging".


*Bangs head* That was it! How was I so blind?

Thanks a million! Rating++!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement