I use wxWidgets 3.1.4 and Windows10
I was trying to use wxThread based on the example taken from https://docs.wxwidgets.org/trunk/classwx_thread.html
I have fixed several things according to some of the conversations in the forum and I was able to compile and run the program.
However, the main thread(GUI) cannot catch the event that should have been posted by the second thread.
I have been stuck for several days. Hope someone in the forum can help.
I have attached a toy project "event_debug.zip" just simply shows the parts of the codes I have problem.
The code below supposed to send an update event every time it finish connecting to server (here, it just waits for two seconds). And, after it finishs five times of connections, it supposed to send a complete event. But, in the main thread, it never catches the event.
Code: Select all
wxThread::ExitCode myThread::Entry()
{
wxLogMessage("======= Start of C++ Socket Client Testing Thread ========");
for (int i = 0; i < 5; i++)
{
wxLogMessage("Connecting to server... Attempt %u\n", i);
Sleep(2000);
wxQueueEvent(m_pHandler->GetEventHandler(), new wxThreadEvent(wxEVT_COMMAND_MYTHREAD_UPDATE));
}
wxLogMessage("======== End of C++ Socket Client Testing Thread ========");
wxQueueEvent(m_pHandler->GetEventHandler(), new wxThreadEvent(wxEVT_COMMAND_MYTHREAD_COMPLETED));
return (wxThread::ExitCode)0; // success
}
Code: Select all
// declare a new type of event, to be used by our MyThread class:
wxDECLARE_EVENT(wxEVT_COMMAND_MYTHREAD_COMPLETED, wxThreadEvent);
wxDECLARE_EVENT(wxEVT_COMMAND_MYTHREAD_UPDATE, wxThreadEvent);
wxBEGIN_EVENT_TABLE(myFrame, wxFrame)
EVT_BUTTON(wxID_BTN_CONNECT, myFrame::OnBtnConnect)
EVT_THREAD(wxEVT_COMMAND_MYTHREAD_UPDATE, myFrame::OnThreadUpdate)
EVT_THREAD(wxEVT_COMMAND_MYTHREAD_COMPLETED, myFrame::OnThreadCompletion)
wxEND_EVENT_TABLE()
wxDEFINE_EVENT(wxEVT_COMMAND_MYTHREAD_COMPLETED, wxThreadEvent);
wxDEFINE_EVENT(wxEVT_COMMAND_MYTHREAD_UPDATE, wxThreadEvent);
Code: Select all
void myFrame::OnThreadCompletion(wxThreadEvent&)
{
wxLogMessage("============== MYFRAME: MyThread exited! ===============");
}
void myFrame::OnThreadUpdate(wxThreadEvent&)
{
wxLogMessage("============== MYFRAME: MyThread update... ===============");
}
Thanks for your help!