Search code examples
wxwidgets

wxWidgets wxMessageDialog works well in a secondary thread while a custom dialog doesn't


I am programming using C++ /CLI with wxWidgets on Windows 11.

I'm Also Using .NET threads


When I create a wxMessageDialog in a secondary thread and run ->ShowModal() , it works.

But when I create a custom dialog in a secondary thread, running ->ShowModal() pops up a wxWidgets debug alert window :

I tried searching on the wxWidgets forum for the same issue but couldn't find one. I found similar issues but none of them addresses my problem.

I also tried to surround MyCustomDialog->ShowModal() with calls to wxMutexGuiEnter(); and wxMutexGuiLeave(); , but that also didn't work.

I also learned about wxThread while scrolling through the forum but if wxMessageDialog works without me having to create a wxThread, I shouldn't have to create a wxThread myself.


I know I shouldn't be calling GUI methods from a secondary thread, but somehow wxMessageDialog's ShowModal() doesnt have a problem with that.

I would like to know how wxMessageDialog works, and if I can implement the same thing with my custom dialog.

Thank you.


Solution

  • In a few words, no there's no way to use a custom dialog from a secondary thread with current implementation and in the foreseeable future.

    Even though your current code works today using wxMessageDialog, there's no guarantee that it will continue to work tomorrow, or on different Windows version or on other OS.

    Some more details:

    • wxMessageDialog and a custom dialog are significantly different internally, even though they represent similar UI features.
    • wxMessageDialog has native implementation, and you can find its wxMessageDialog::ShowModal() implementation in src/msw/msgdlg.cpp.
    • A custom dialog will be assembled an run by wxWidgets code, even though the dialog itself and other components are native widgets. You can find wxDialog::ShowModal() in src/msw/dialog.cpp.

    If you really want to pursue some experimental development (because it would still not be suited for anything else), and being on Win 11, you could try wxRichMessageDialog. It provides more features than wxMessageDialog and on Vista and later versions it has native implementation, which might go along with your "hack".

    But again, using UI from secondary threads will lead to a dead end sooner or later.