How to show Cdialog with std::async

246 Views Asked by At

I have a derive class named A was inheritance from CDialog, I created the object to A named a and want to utilize the member function domodal to show dialog. Nonetheless, this dialog cannot show and parent window was block.

A a(this);

auto DlgResult = std::async(std::launch::async, &A::DoModal,&a);

DlgResult.wait();

if (DlgResult.get() == IDOK)
{
    std::wstring ss = a.get_text_fromdlg();
}

Can someone help me, thanks!

1

There are 1 best solutions below

1
Nick Delbar On

If I were you, I wouldn't wrestle w/ the Async and DoModal since the purpose of DoModal() is to wait for the response from the dialog to let the app know how to move forward..

Below, I've added a simpler option. Just create member variable pointer to a Dialog class, and then use Show Window. Also, in this instance, you may consider making the dialog topmost so you don't lose focus of it.

MFCClass1* m_pDlg = new MFCClass1();

void CMFCApplication1Dlg::OnBnClickedButton1()
{
    m_pDlg->Create(IDD_DIALOG1);
    m_pDlg->ShowWindow(SW_SHOWNORMAL);
    SetWindowPos(&m_pDlg->wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}

enter image description here