2016-12-31 132 views
1

我想顯示一個對話框來通知用戶應用程序正忙。爲了避免阻塞主線程,我想使用std ::線程來顯示對話框。請看下面的代碼:在std :: thread中打開MFC對話框

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

看一看:http://stackoverflow.com/questions/1669017/how-to-create- a-mfc-dialog-with-a-progress-bar-in-a-separate-thread – dwo

+0

是否刪除inProcess;甚至編譯?無論如何,放棄它 - 這是沒有道理的。 –

+1

反過來做它更好,通常也更容易 - 在主UI線程上顯示對話框,在工作線程上運行繁忙處理。 –

回答

1

我能想到的兩種方式做到這一點:

無模式對話框

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

用戶線程(UI線程)

使用CWinThread創建一個兄弟到主UI線程(CWinApp)。最重要的是分配一個指向一個對話框的CWinThread :: m_pMainWnd成員。如果對話框是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文件**

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; 
} 
+0

恐怕,這不會像[本評論]中所解釋的那樣工作(http://stackoverflow.com/questions/41407418/open-a-mfc-dialog-in-a-stdthread?noredirect=1#comment70025977_41407418 )。 – IInspectable