Click to See Complete Forum and Search --> : Creating a message loop in a Modal dialog box
JimG
May 5th, 2004, 01:23 PM
I have an MFC dialog based app.
The dialog creates a worker thread.
The worker thread periodically calls PostMessage with status.
I want the dialog to display these status messages in a simple edit box.
Normally, message handling is done in the OnIdle() loop, but since the app is dialog based (My code is all in a CDialog derived class), I don't have that loop. The CWinApp class launches the dialog through a DoModal(), so it's OnIdle() never executes.
I can create my own endless loop to look for messages (using PeekMessage()), then go to sleep between messages, but that pretty much defeats the purpose of having the app multi-threaded.
Is there something simple that I'm missing?
kirants
May 5th, 2004, 05:05 PM
I don't understand the issue. DoModal itself implements a message loop. So, if any you post any message to the dialog, the doModal loop should get it.
JimG
May 5th, 2004, 05:19 PM
I'm adding my own messages. I want to trap those.
Usually, I do this by overriding the OnIdle loop, and doing a PeekMessage there. Since the class is a CDialog, not a CWinApp, it doesn't have an OnIdle loop.
Where does the message loop exist? Is it in a function I can override in the Dialog class?
kirants
May 5th, 2004, 05:20 PM
Are you doing a PostMessage to the dialog handle ?
JimG
May 5th, 2004, 05:47 PM
Yes.
PostMessage(AfxGetMainWnd()->m_hWnd, USER_DEFINED_MESSAGE1, 0, 0)
Then in my dialog, I've written a loop that I call as soon as I start my thread.
while (thread_running)
{
if (PeekMessage(msg, ...))
{
switch (msg.message)
{
<translate USER_DEFINED_MESSAGES to strings>
}
m_ResultEdit.SetWindowText(strMessage);
}
else
Sleep(200);
}
It's this loop that, of course, takes over the UI thread.
kirants
May 5th, 2004, 05:53 PM
I don't think there is a necessity to do a while loop.
Can you try the following:
1. Keep the postmessage stuff in the other thread as is.
2. In your dialog's message map add
ON_MESSAGE(USER_DEFINED_MESSAGE1,OnUserDefinedMessage1)
3. To your dialog header add the handler as
LRESULT OnUserDefinedMessage1(WPARAM wParam,LPARAM lParam);
4. add
LRESULT YourDialog::OnUserDefinedMessage1(WPARAM wParam,LPARAM lParam)
{
m_resultEdit.SetWindowText("test");
}
If this works ok, then probably I know the reason why it wasn't before
JimG
May 5th, 2004, 05:57 PM
I think that's gonna do it. I just remembered/found-documentation on the message map right before I read your message.
JimG
May 5th, 2004, 06:10 PM
Of course that worked.
So, to answer my original question...
Yes, I was missing something simple.
Thanks for the help, again.
kirants
May 5th, 2004, 06:14 PM
Just for the fun of it, try this in your while loop u had earlier..
if(PeekMessage())
{
switch( user defined message)
{
case 1: m_resultEdit.SetWindow....
break;
default:
{
TranslateMessage()
DispatchMessage()
}
break;
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.