Click to See Complete Forum and Search --> : Does DoModal create a thread?


lsvedin
December 16th, 2004, 04:34 PM
I really hate being a bother, but here's the problem. I have two threads up and running, a main and a worker. I am only passing data between them using PostThreadMessage.

The main thread kick off the worker thread class and then the main thread pops up a modal dialog box. The dialog box does a POSTTHREADMESSAGE to the worker thread, the thread does its thing, and then it does a POSTTHREADMESSAGE back to the main thread. Here comes in the problem, the main thread does a POSTMESSAGE to the dialog and the app does all sorts of funny things.

Looking in the debugger, the call stack has this

CWnd::PostMessageA(unsigned int 1039, unsigned int 16387, long 0) line 41 + 43 bytes
MyApp::SendToMyDialog(unsigned int 16387, long 0) line 347
CWinThread::DispatchThreadMessageEx(tagMSG * 0x0042d1f8 {msg=0x0000040f wp=0x00004003 lp=0x00000000}) line 652
CWinThread::PreTranslateMessage(tagMSG * 0x0042d1f8 {msg=0x0000040f wp=0x00004003 lp=0x00000000}) line 660 + 20 bytes
CWinThread::PumpMessage() line 841 + 30 bytes
CWnd::RunModalLoop(unsigned long 4) line 3478 + 19 bytes
CDialog::DoModal() line 536 + 12 bytes
MyView::DoThat(unsigned int 1031) line 649 + 11 bytes
MyApp::DoThis(unsigned int 0, long 0) line 307

It appears that the dialog is running in its own thread and so I believe that I can't do a myDlg->PostMessage() since its a CWnd function.

I tried a ::PostMessage(myDlg.m_hwnd, data1, data2), but the handle was NULL and I tried a GetSafeHwnd, but that returned a NULL.

Will the latter solve the problem if I get a valid handle and if so, how do I get a valid handle?

Arjay
December 17th, 2004, 12:23 AM
The main thread kick off the worker thread class and then the main thread pops up a modal dialog box.I believe the modal dialog is preventing the main app from receiving the messages (modal dialogs have their own message pump). Can you answer why your main thread pops up the main dialog? What are you trying to do? I ask because there might be another way to accomplish it. A modeless dialog or maybe having the modal dialog spawn and wait for the thread itself.

Arjay

Andreas Masur
December 17th, 2004, 03:23 AM
[ Moved thread ]

lsvedin
December 17th, 2004, 09:41 AM
The worker thread is sending and getting information from another computer and sending some of the received data back to the main thread. The dialog box is used to get data from the user that is needed by the other computer to know what data to send us.

The problem arises when the other computer has determined that the data the user has entered is invalid and I have to let the dialog know this in order to notify the user of an "Id Ten T" (Id10T :D ) error ocurrance. This is where the problem creeps in.

Arjay
December 17th, 2004, 01:00 PM
The worker thread is sending and getting information from another computer and sending some of the received data back to the main thread. The dialog box is used to get data from the user that is needed by the other computer to know what data to send us.Rather than have the thread send data back to the main thread, could you send it to the dialog instead?

Do something like:

// In main thread
Create dialog instance
Create your thread (passing the dialog instance pointer as the lParam to the thread)
Call DoModal on the dialog
When the data needs to be sent inside the thread convert the lParam back to a dialog pointer and use pDlg->SendMessage to send a user message to the dialog.

If you don't want the thread to begin processing until the dialog is opened, you can start the thread as suspended in the main thread, pass the thread handle to the dialog and then call resume thread in the dialog's OnInitDialog() method.

Arjay

lsvedin
December 17th, 2004, 01:26 PM
Man do I feel stupid. :rolleyes: :blush:

In my main app, there is a member variable for the dialog. No problem so far.

CMyDlg m_MyDlg;

Now, in the main view when I do a DoModal, I create a local variable to the dialog in a function and kick it off.

CMyDlg myDlg;
myDlg.DoModal();

This is a problem especially if whenever I want to talk to the dialog the user is user and I'm trying to talk to m_MyDlg.

Well, so much for many hours of harsh debugging and pulling my hair out. :confused: Well, my hair will hopefully grow back.

Hey, it's not the first time I made a stupid coding mistake and it won't be the last.

Thanks Arjay. :thumb: