Multi-thread application crash when wxMessageBox called. Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
congminh60
Knows some wx things
Knows some wx things
Posts: 39
Joined: Sun Oct 10, 2010 1:02 am

Multi-thread application crash when wxMessageBox called.

Post by congminh60 » Sun Oct 10, 2010 1:18 am

Hi all,

I have an multi-threaded application, which have following description:

In class CMyThread (derived from wxThread):

Code: Select all

    void* CMyThread::Entry()
    {
        while(true)
        {
            wxMessageBox("Tick");
            Sleep(3000);
        }
        return NULL;
    }
In class CMyFrame (derived from wxFrame)

Code: Select all

    CMyFrame::CMyFrame(const wxString &title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(328,150), wxSTAY_ON_TOP | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN)
    {
        CMyThread* pThread = new CMyThread();
        pThread->Create();
        pThread->Run();
    }
When run that program, application crashed.

Sorry, i forgot to notice that class CMyThread is in a another Dynamic Library. See the attached file.
Attachments
MTThread.rar
(12.38 KiB) Downloaded 30 times

eranif
Moderator
Moderator
Posts: 607
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif » Sun Oct 10, 2010 5:31 am

All GUI calls must be done from the main thread.

If you want to notify the user about something, you should communicate it to the main thread by posting an event and only it can call wxMessageBox

If you dont know how to post an event between threads, search this forum there are many of examples here

An example of threads management:
http://forums.wxwidgets.org/viewtopic.php?t=19315
Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org

congminh60
Knows some wx things
Knows some wx things
Posts: 39
Joined: Sun Oct 10, 2010 1:02 am

Post by congminh60 » Sun Oct 10, 2010 8:04 am

Thankyou Eran, its work.

Post Reply