1

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!

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Savner_Dig
  • 21
  • 1
  • 8
  • 2
    Keep all GUI activities in a single thread. – seccpur Dec 17 '19 at 02:04
  • @seccpur Thank you for your replay, I will remember it. I just have an attempt to make it asynchronism. – Savner_Dig Dec 17 '19 at 02:09
  • 1
    Do you want specifically `std::async`, or do you merely want a non-blocking dialog? MFC predates `std::async`, and has other tools for keeping CDialog from blocking the UI thread. This SO question has some examples: https://stackoverflow.com/questions/2271821/how-to-display-a-non-modal-cdialog – parktomatomi Dec 17 '19 at 02:14
  • @parktomatomi Currently, The dialog cannot show if I use std::async, I want to use std::async to show dialog.... – Savner_Dig Dec 17 '19 at 02:17
  • @parktomatomi Thanks for your replay, it's just an attempt. Actually, I'm not means that I will absolutely use it. – Savner_Dig Dec 17 '19 at 02:30
  • (whoops, I think I deleted my comment and reposted after you replied) @Savner_Dig Thanks for entertaining my question. I'm just curious, why use `std::async` in the first place, instead of running CDialog on the same thread modelessly? `std::async` with `std::launch_async` executes the task on a thread pool. But that's usually for doing CPU-intensive tasks. – parktomatomi Dec 17 '19 at 02:30
  • 2
    For non-blocking dialog, use a Modeless dialog. – seccpur Dec 17 '19 at 02:31
  • Advice: don't use `std::async` for that. You also should describe how exactly these dialogs are supposed to work (modal, modeless) – Jabberwocky Dec 17 '19 at 07:57

1 Answers1

0

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

Nick Delbar
  • 111
  • 1
  • 2
  • 9
  • That's not how you ensure, that a dialog is always in front of its owner. You simply pass the owning window into `CDialog`'s [c'tor](https://learn.microsoft.com/en-us/cpp/mfc/reference/cdialog-class#cdialog) (that unfortunately goes by the name `pParentWnd`). Done. – IInspectable Dec 17 '19 at 15:34