Sign in to follow this  

win32 thread message loop won't work.

This topic is 4735 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

Having finally got threads to cooperate with me under win32, I've found that giving them their own message loop and posting them messages is also causing me problems. I've read that unless you do certain things to them, windows assumes they are going to be worker threads only and don't give them a message queue. The MSDN documentation mention that you must call a GDI or User interface function in order for a message queue to be created. At the moment I have the following:
includes etc,

DWORD thrId;

void CALLBACK midiCallBack(HMIDIIN handle, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2) {
	...
	if(!PostThreadMessage(thrId, WM_USER + 1, NULL,NULL)) {
	if(GetLastError()==ERROR_INVALID_THREAD_ID) OutputDebugStr("It's broken\n");
	}
        ...
}

void ThreadFunc(void* m) { 
	int t = 0;
	
	MSG msg2;

	for(;;) {
		Sleep(0);
		if(PeekMessage(&msg2,NULL,0,0,PM_REMOVE)) {
			rend->beat();
		
		}
	}
}

int WINAPI WinMain(HINSTANCE hInst,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	rend = new CRenderer;

	int time = 0;
	thrId = _beginthread(ThreadFunc,0,NULL);
	
	// Main message loop:
	MSG msg;
	BOOL done = FALSE;
	if(!CreateGLWindow("MidiLights",640,480,16,false)) return 0;
	int t = 0;
	
	while(!done) {
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
			if (msg.message==WM_QUIT) done=TRUE;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else {
			if (active && (timeGetTime() - t) > 20) {
				if(!rend->DrawGLScene() || keys[VK_ESCAPE]) done = TRUE;
				SwapBuffers(hDC);
			}
			t = timeGetTime();
		}
	}

	return 0;
}


This is a cut down excerpt from my source. All the usual boiler plate has been removed. The effect I want is that whenever a midi message is received, the thread should receive the message WM_USER+1, and as a result call the beat() method of the rend object. The method midiCallback is called whenever a MIDI message is received, and this works. The response I get each time a midi event happens is that 'It's broken' is printed, indicating that my thread Id is somehow invalid. I'm quite new to all this, so I've probably done something really silly. Can anyone help me out? //lessbread: changed code tags to source tags [Edited by - LessBread on December 30, 2004 9:15:30 PM]

Share this post


Link to post
Share on other sites
Are you confident that the thread id returned by beginthread is the needed thread id? Try getting the thread id from within the thread function using GetCurrentThreadId(); and see if that makes a difference.

And do you plan to ever exit the loop in the thread function? You might consider checking the message for WM_QUIT or similar and exiting the loop when it comes through.

As far as calling a GDI/User32 function in the thread goes, PeekMessage should do the trick.

The docs that I have for PostThreadMessage say that it might be necessary to call the function repeatedly in order for it to succeed.

Here's a snippet from some code of mine that illustrates this:


if ( g_tid != 0 ) {
while ( !PostThreadMessage(g_tid,WM_QUIT,0,0) ) { Sleep(10); }
}

Share this post


Link to post
Share on other sites

This topic is 4735 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.

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