Confusion with wxCommandEvent and wxThreadEvent 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
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Confusion with wxCommandEvent and wxThreadEvent

Post by MagickPanda »

I am testing the solution doublemax kindly gave me in my other topic, but I failed at using wxThreadEvent to 'signal' my mainthread wxframe to do a certain function call.

I think wxCommandEvent should work too if I avoid passing user data from non-main thread to mainthread by using shared data between threads using wxMutex's.

This is how I am going 'abuse' wxCommandEvent usage:
1.Share data between threads.
2.Create custom wxCommandEvent and set an int value as userinfo/index/id in the event I created
3.Send the event via ProcessWindowEvent
4.Mainthread call my custom function upon processing my custom event

Though I just want to know if there are any logic errors or thread-safe related pitfalls in my method. Cause I can't seem to find a simple and proper way to use wxThreadEvent to create custom event, and information related to wxThreadEvent derived class event samples/related-threads are scarce.

Thanks again.

edit:nevermind -.-
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Confusion with wxCommandEvent and wxThreadEvent

Post by eranon »

Hello,

In the past (see https://groups.google.com/forum/#!topic ... bByxAvdio0) wxThreadEvent was derived from wxCommandEvent, so it was just a specialization... But now, both are independents (both derived from wxEvent).

wxCommandEvent is supposed to be emitted by a control, while a wxThreadEvent is a free helper at you convenience. In my apps, I use both, talking about inter-threads communication, and my choice for one or another, most of the time, simply come from the answer to these questions: What data I have to transmit (eg. wxThreadEvent has SetPayload() to send everything wxAny compliant with thread-safe copy constructor) from the worker thread to the main one? And, eventually, does this event look like or replace an action which could be done from a control in the main (GUI) thread?

About inter-thread communication, you have this page as an overview of different use cases: https://wiki.wxwidgets.org/Inter-Thread ... munication.

And also, I think this one https://wiki.wxwidgets.org/Custom_Events is interesting too, to well understand the difference between event class and event type; where it's said:
What sort of custom event do I need?

The term 'custom event' can be used to mean either creating your own subclass of wxEvent or wxCommandEvent, or just defining a new wxEventType to use with the standard event classes. Which of these you do depends on how much, and what type of, data you want to send in your event.

Most of the time you won't need a subclassed wxEvent, just a standard event with a new wxEventType to 'label' it. Even a plain wxEvent can transport useful data in its id and eventType fields. A wxCommandEvent additionally has fields for another int, a long int and a wxString. It can also take a void* in its clientData field. All these should be more than adequate in most situations, so needing to create your own custom class is rare. However if you do need to transport lots of data or have other special needs, subclassing is available and gives you the flexibility to do whatever is required.
EDIT: About the way to send the custom event from worker thread to main GUI one, I'm used to go with wxQueueEvent which is thread-safe for sure.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Re: Confusion with wxCommandEvent and wxThreadEvent

Post by MagickPanda »

Thanks for the long explanation.

After digging from the docs and the helpful replies and archived threads of this forums, I found that wxThreadEvent is a specialized version of wxEvent, though using wxThreadEvent seemed complicated things for me.

I reversed to using wxCommandEvent based custom event to send and process event in wx message queue, and it seems it's a more straight-forward solution than using wxThreadEvent, which is less documented and a bit confusing for me.

Here is how I am using the wxCommand event to generate and process custom event between threads, feel free to point out the dumb mistake I make though, so I could fix it. :lol:

Code: Select all

g_Frame->SendEventSTMA(index);

Bind(STMA_EVENT, &MyFrame::OnSTMA, this, GetId());

void MyFrame::SendEventSTMA(int index)
{

	wxCommandEvent *eventCustom = new wxCommandEvent(STMA_EVENT, GetId());

	eventCustom->SetEventObject(this);
	eventCustom->SetInt(index);
	eventCustom->SetString("STMA");

	wxQueueEvent(this, eventCustom);
}

void MyFrame::OnSTMA(wxCommandEvent& event) {
	int slot = event.GetInt();
	
	this->runStrat(this->getSTMA(slot), slot);
}
edit:updated code
Last edited by MagickPanda on Sat Jun 17, 2017 6:13 am, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Confusion with wxCommandEvent and wxThreadEvent

Post by doublemax »

What exactly is the problem, what doesn't work?

Code: Select all

void MyFrame::OnEventSTMA(CCEvent evt)
Is this real code? This event handler shouldn't get called at all, because the signature is wrong.

Apart from that, everything looks ok to me.
Use the source, Luke!
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Re: Confusion with wxCommandEvent and wxThreadEvent

Post by MagickPanda »

doublemax wrote:What exactly is the problem, what doesn't work?

Code: Select all

void MyFrame::OnEventSTMA(CCEvent evt)
Is this real code? This event handler shouldn't get called at all, because the signature is wrong.
oops, I think I pasted old code, the code is working fine, but I am not 100% sure I am using the queue and event correctly.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Confusion with wxCommandEvent and wxThreadEvent

Post by doublemax »

but I am not 100% sure I am using the queue and event correctly.
Looks fine to me.
Use the source, Luke!
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Re: Confusion with wxCommandEvent and wxThreadEvent

Post by MagickPanda »

doublemax wrote:
but I am not 100% sure I am using the queue and event correctly.
Looks fine to me.
Thanks. :D
Post Reply