wxThreadEvent trouble

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.
HackiWimmer
In need of some credit
In need of some credit
Posts: 2
Joined: Tue Oct 03, 2017 1:52 pm

wxThreadEvent trouble

Post by HackiWimmer »

. . . It try to send a wxThreadEvent from a second thread to the main thread, but the corresbonding event handler doesn't catching this event. If I use a wxCommandEvent instead and it works well. Here my code fragments:

mainframe.h

Code: Select all

wxDECLARE_EVENT(wxEVT_UPDATE_MANAGER_THREAD_COMPLETED, wxThreadEvent);
wxDECLARE_EVENT(wxEVT_XXX, wxCommandEvent);
mainframe.cpp

Code: Select all

wxDEFINE_EVENT(wxEVT_UPDATE_MANAGER_THREAD_UPDATE, wxThreadEvent);
wxDEFINE_EVENT(wxEVT_XXX, wxCommandEvent);

wxBEGIN_EVENT_TABLE(MainFrame, MainFrameBClass)
	EVT_COMMAND(wxID_ANY, wxEVT_XXX, MainFrame::testFunction2)
	EVT_THREAD(wxEVT_UPDATE_MANAGER_THREAD_UPDATE, MainFrame::onThreadUpdate)
wxEND_EVENT_TABLE()

wxThread::ExitCode UpdateManagerThread::Entry() {

	while ( !TestDestroy() ) {
			
		wxCommandEvent evt1(wxEVT_XXX);
		pHandler->AddPendingEvent(evt1);
		
		wxThreadEvent evt2(wxEVT_UPDATE_MANAGER_THREAD_UPDATE);
		pHandler->AddPendingEvent(evt2);
       }
}
evt1 arrives
evt2 not

What is going wrong.

Many Thanks . . .
Last edited by doublemax on Tue Oct 03, 2017 3:57 pm, edited 1 time in total.
Reason: Added code tags
User avatar
doublemax
Moderator
Moderator
Posts: 19164
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxThreadEvent trouble

Post by doublemax »

Code: Select all

wxDECLARE_EVENT(wxEVT_UPDATE_MANAGER_THREAD_COMPLETED, wxThreadEvent);
...
wxDEFINE_EVENT(wxEVT_UPDATE_MANAGER_THREAD_UPDATE, wxThreadEvent);
Is this a copy-paste error? You need a define and declare for each event type.

However, with these macros you define a new event *type*. But the first parameter to the EVT_THREAD macros is an ID. Try wxID_ANY there (or the real id of the control that receives the event):
Use the source, Luke!
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: wxThreadEvent trouble

Post by coderrc »

HackiWimmer wrote:

Code: Select all

wxBEGIN_EVENT_TABLE(MainFrame, MainFrameBClass)
	EVT_COMMAND(wxID_ANY, wxEVT_XXX, MainFrame::testFunction2)
	EVT_THREAD(wxEVT_UPDATE_MANAGER_THREAD_UPDATE, MainFrame::onThreadUpdate)
wxEND_EVENT_TABLE()

if what you are looking for is something like

Code: Select all

wxBEGIN_EVENT_TABLE(MainFrame, MainFrameBClass)
	EVT_UPDATE_MANAGER_THREAD_UPDATE(wxID_ANY, MainFrame::onThreadUpdate)
wxEND_EVENT_TABLE()
then have a look at
https://wiki.wxwidgets.org/Custom_Events#Subclassing