Search code examples
c++wxwidgetsmessagebox

wxPanel show function does not work inside wxThreads


#include "MessageBoxThread.h"

MessageBoxThread::MessageBoxThread(NfcUIConfirmProcessUC* dialogUC)
{
    this->dialogUC = dialogUC;
}

MessageBoxThread::~MessageBoxThread(void)
{
}

void* MessageBoxThread::Entry()
{
    this->dialogUC->Show(true);
    return 0;
}

Solution

  • To quote from the wxThread docs:

    GUI calls, such as those to a wxWindow or wxBitmap are explicitly not safe at all in secondary threads and could end your application prematurely. This is due to several reasons, including the underlying native API and the fact that wxThread does not run a GUI event loop similar to other APIs as MFC. [...] the recommended way is to simply process the GUI calls in the main thread through an event that is posted by wxQueueEvent()

    So basically the best way to solve this problem is rather than calling Show to create an event (for example a wxThreadEvent) post it back to the dialog and then using an event macro or bind connect it to a dialog member which shows the dialog.