在std :: thread中打开MFC对话框

时间:2016-12-31 10:19:16

标签: c++ mfc dialog stdthread

我想显示一个对话框,通知用户应用程序正忙。为了避免阻塞主线程,我想使用std :: thread来显示对话框。请考虑以下代码:

InProcDlg inProcess;
std::thread t([ &inProcess ] {      
    inProcess.DoModal();
    delete inProcess;
});
// wait till process fished 
::PostMessage(inProcess.m_hWnd, WM_USER + 1, 0, 0);
if (t.joinable()){
    t.join();
}

InProcDlg.cpp

BEGIN_MESSAGE_MAP(InProcDlg, CDialogEx)
  ...
  ON_MESSAGE(WM_USER + 1, &InProcDlg::close)
END_MESSAGE_MAP()

LRESULT InProcDlg::close(WPARAM wParam, LPARAM lParam)
{
  UNREFERENCED_PARAMETER(wParam, lParam);
  EndDialog(1);
  return 0;
}

运行此代码,对话框显示正确。该对话框也已关闭,但未显示主对话框,应用程序将挂起CreateRunDlgIndirect()。尝试介入时,在设置一些断点时,主对话框会再次正确显示。很奇怪。对于我需要深入研究的任何建议,我会非常高兴。

在下一步中,我还想通过发送一个指示当前进程状态的整数来向用户显示该进程。

int *percent;
::PostMessage(inProcess.m_hWnd, WM_USER + 2, 0, reinterpret_cast<LPARAM>(percent));

在发送或发布消息之前,如何获取对话已存在的证据? 我使用的是Visual Studio 2013。

1 个答案:

答案 0 :(得分:1)

我可以想到两种方法:

无模式对话

https://www.codeproject.com/Articles/1651/Tutorial-Modeless-Dialogs-with-MFC

用户线程(UI线程)

使用CWinThread为主UI线程(CWinApp)创建兄弟。最重要的是为CWinThread :: m_pMainWnd成员分配一个指向Dialog的指针。如果对话框是Modal,则在调用DoModal后立即返回FALSE,并返回TRUE for Modeless。

class CMainFrame : public CFrameWnd {
    // pointer to thread
    CWinThread* m_pUserThread;
}

启动帖子

m_pUserThread = AfxBeginThread(RUNTIME_CLASS(CUserThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
m_pUserThread->m_bAutoDelete = TRUE;
m_pUserThread->ResumeThread();

headr file **

class CUserThread : public CWinThread
{
    DECLARE_DYNCREATE(CUserThread)
public:
    CUserThread();

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CUserThread)
    public:
    virtual BOOL InitInstance();
    virtual int ExitInstance();
    //}}AFX_VIRTUAL

protected:
    virtual ~CUserThread();
    // Generated message map functions
    //{{AFX_MSG(CUserThread)
        // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    CUserMsg* m_pDlg;
}

源文件

#define DO_MODAL
BOOL CUserThread::InitInstance()
{

    #ifdef DO_MODAL
    // create and assign Modeless dialog
    CUserMsg dlg;
    m_pMainWnd = &m_pDlg;
    dlg.DoModal();
    return FALSE;
    #endif

    #ifdnef DO_MODAL
    // create and assign Modal dialog
    m_pDlg = new CUserMsg();
    m_pDlg->Create( IDD_USER_DLG, AfxGetMainWnd() );
    m_pMainWnd = m_pDlg;
    return TRUE;
    #endif
}

int CUserThread::ExitInstance()
{
    // check for null
    m_pDlg->SendMessage( WM_CLOSE ) ;
    delete m_pDlg;

    return CWinThread::ExitInstance();
}

终止线程

// post quit message to thread
m_pUserThread->PostThreadMessage(WM_QUIT,0,0);

// wait until thread termineates
::WaitForSingleObject(m_pUserThread->m_hThread,INFINITE);

对于这两种方式,我强烈建议将对话框作为最顶层的窗口:

BOOL CLicenseGenDlg::OnInitDialog() {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here
    SetWindowPos( &wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | WS_EX_TOPMOST );
    return TRUE;
}