I do it the following way:
...
class CConnMsgDlg : public CDialog
{
...
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CConnMsgDlg)
virtual BOOL OnInitDialog();
...
//}}AFX_MSG
afx_msg LRESULT OnEnd(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
...
private:
static const UINT MY_WM_END;
};
...
/////////////////////////////////////////////////////////////////////////////
// Message table
const UINT CConnMsgDlg::MY_WM_END =
RegisterWindowMessage(_T("MY_WM_END-{29813B84-DD1E-11d7-9B13-0002B32C4875}"));
BEGIN_MESSAGE_MAP(CConnMsgDlg, CDialog)
//{{AFX_MSG_MAP(CConnMsgDlg)
...
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE(MY_WM_END, OnEnd)
END_MESSAGE_MAP()
BOOL CConnMsgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
// Create the thread and start it
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
LRESULT CConnMsgDlg::OnEnd(WPARAM wParam, LPARAM lParam)
{
EndDialog(wParam);
return 0L;
}
void CConnMsgDlg::ThreadFnc()
{
...
PostMessage(MY\_WM\_END, IDOK); // destroy the dialog
}
I have a dialog that starts a thread in the 'OnInitDialog' function. The thread reads some data from the serial port. This can take a long time, so I do it in a own thread. When the thread is ready it post the message 'MY_WM_END', that I have registered in the message map. The 'OnEnd' function closes the dialog with 'EndDialog'. Cheers, Daniel. -- FIND A JOB YOU LOVE, AND YOU'LL NEVER HAVE TO WORK A DAY OF YOUR LIFE. ;)