Displaying a CDialog using messages from a worker thread intermittently fails to display modal windo

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a SDI MFC/CLR app written in VS2008 SP1. We have a worker thread that performs some FTP downloads, then displays a "completed" message using our CMyDialog derived from CDialog.

This appears to work well, but intermittently an invisible dialog appears. I see the mainframe disabled as you would expect when a child dialog is modal, but the child dialog is not visible anywhere. According to Spy++ the dialog window has been created and the parent Window handle matches the mainframe. When the mainframe is clicked, pressing ENTER on the keyboard fires the OK button on the hidden dialog and all continues as expected.

The question is, why would a CDialog not be painted? Is there something wrong with our approach? Weve tried to be as thread safe as possible by displaying UI using messages that are processed by the mainframe.

Heres our implementation...

struct MYDIALOGINFO
{
CMyDialog *pDialog;
INT_PTR nId;
};

** WORKER THREAD **

g_hEvent = CreateEvent(NULL, TRUE, FALSE, TEXT("MyUIEvent"));

// Create dialog
MYDIALOGINFO *pDialogInfo = new MYDIALOGINFO;
pDialogInfo->pDialog = new CMyDialog(theApp.m_pMainWnd);
pDialogInfo->nId = 0;

// Send display dialog message to MainFrame
theApp.m_pMainWnd->PostMessage(WM_MY_DISPLAYDIALOG, (WPARAM)pDialogInfo, (LPARAM)NULL);

// Wait for exit event
DWORD dwWaitResult = WaitForSingleObject(g_hEvent, INFINITE);

// Reset event signal
ResetEvent(g_hEvent);

// Handle dialog response....

// Cleanup
delete pDialog;
delete pDialogInfo;


** CMAINFRAME **

ON_MESSAGE(WM_MY_DISPLAYDIALOG, OnDisplayDialog)

LRESULT CMainFrame::OnDisplayDialog(WPARAM wParam, LPARAM lParam)
{
MYDIALOGINFO *pDialogInfo = reinterpret_cast<MYDIALOGINFO*>(wParam);

// Display dialog (calling process must handle cleanup)
pDialogInfo->nId = pDialogInfo->pDialog->DoModal();

// Signal UI has closed
SetEvent(g_hEvent);

return 0;
}

Any help or sanity checking would be appreciated.

Thanks.

View the full article
 

Similar threads

S
Replies
0
Views
287
Suresh Kansujiya
S
Back
Top