Click to See Complete Forum and Search --> : User interface threads & messages getting lost


BrianOG
May 6th, 1999, 03:56 AM
Hi all,

I have a (modeless) dialog running in a userinterface thread. I am sending messages to the dialog using PostThreadMessage. If I leave the dlg alone this works fine, all messages get through and are processed. However if I try to move the dlg while the messages are comming through (by clicking on title bar and dragging) a large number of messages are lost.
I have tried adding my own PostThreadMessage function as follows:
BOOL CGraphThread::MyPostThreadMessage(UINT uMessage, WPARAM wParam, LPARAM lParam)
{
while ( !CWinThread::PostThreadMessage(uMessage, wParam, lParam) )
Sleep(0);
return true;
}



But even this doesnt work. Its a bit of an improvement - if I click on the titlebar and hold it down, the message are not processed, but when I release the mouse button, they are processed then, but if I drag the titlebar, then the messages posted during the drag are NOT processed.

I would like not to have to resort to having to waiting on an event until the message has been processed as the system I am writing is a real(ish)-time system, and I cant hold up my process while waiting for the dlg to process the messages.

Regards,
Brian

Marqy
May 6th, 1999, 04:06 AM
I haven't used the UI threads, so I'm not sure what your problem is. However, I've implemented a similar system for some serial COMMS stuff; if you create a dialog in the main thread (modal or modeless), then in your ::OnInitDialog() create a worker thread using ::AfxBeginThread() your problem should be solved as the dialog message queue is in a separate thread from your worker message queue (i.e. UI actions in the dialog have no impact in the thread).

MJA

BrianOG
May 6th, 1999, 04:56 AM
This isn't really possible in the setup I have.
My main application launches a worker thread to run tests on hardware (via HP-IB or FireWire interface). These tests in turn bring up a dialog to display a graph of results etc. Since this dialog is UI, I am creating a user-interface thread and creating/launching the dialog with in this thread. My worker thread then posts messages to the user-interface thread to add data to the dialog. However, when I try to drag or resize the dialog, the thread messages posting the data to the dlg are getting lost (even though they are being reported as being posted). Also, since I am passing poiners to structures allocated on the the heap, these are not being deleted, becuase the message is not being processed and I am getting loads of memory leaks.

BrianOG
May 10th, 1999, 07:49 AM
I've found the problem and a solution, so here it is for anyone else who comes across this.....

Beacuse window messages are posted directly to the dialog, and not processed through the message queue, they are given priority over my custon thread messages. (I am still not sure why the thread messages weren't stored in the queue). To overcome this I had to override the ProcessMessageFilter of CWinThread, and check for my messages, and call the appropriate message handlers, everything else was let throught to the message queue. This way I was able to grab my messages before windows got a chance to filter the window messages directly to the window...

happy threading...