Any times to avoid using wxThreadEvent, or custom event class returning wxEVT_CATEGORY_THREAD from GetEventCategory()?

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
darlingm
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Apr 11, 2015 7:27 pm

Any times to avoid using wxThreadEvent, or custom event class returning wxEVT_CATEGORY_THREAD from GetEventCategory()?

Post by darlingm » Wed Apr 29, 2015 8:51 am

For events I'm generating, are there situations I would NOT want GetEventCategory() to return wxEVT_CATEGORY_THREAD? I spent a bit of time digging into YieldFor(), ProcessPendingEvents(), etc, and realized that's a deep rabbit hole.

I see wxThreadEvent always returns this to prevent re-entrant type problems, and events from being processed out of order.

If your program uses threads in some situations, is it best to use an event that returns this event category too? (Whether you choose to use or derive from wxThreadEvent, or create your own class derived from wxEvent that overrides GetEventCategory() too.)

Or, are there inter-function communications that are NOT also inter-thread communications, when you would want to return a different event category?

For this question, I'm not concerned about the additional expense needed of deep copying objects such as wxStrings to be thread safe... Just focused on the event category aspect.

catalin
Moderator
Moderator
Posts: 1529
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Any times to avoid using wxThreadEvent, or custom event class returning wxEVT_CATEGORY_THREAD from GetEventCategory(

Post by catalin » Wed Apr 29, 2015 10:21 am

All events will be processed by the main thread (since that is where the event loop runs).
wxThreadEvent is an event you would emit from a secondary thread.
Should you emit events from the main thread, you can use any event type that suits your needs (well, you might want to use a custom event type to avoid confusion). But it would not matter if the program used threads in some situations or not.

darlingm
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Apr 11, 2015 7:27 pm

Re: Any times to avoid using wxThreadEvent, or custom event class returning wxEVT_CATEGORY_THREAD from GetEventCategory(

Post by darlingm » Wed Apr 29, 2015 8:34 pm

Do I want to have the main thread never emit a wxThreadEvent? Are there situations where emitting a wxThreadEvent from the main thread is OK, but situations I'd want to avoid it?

darlingm
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Apr 11, 2015 7:27 pm

Re: Any times to avoid using wxThreadEvent, or custom event class returning wxEVT_CATEGORY_THREAD from GetEventCategory(

Post by darlingm » Wed Apr 29, 2015 8:42 pm

In other words, if I create a class called myEventClassInt, which derives from wxEvent, and its additional functionality is to carry an int along with the event...

Do I want to have a myEventClassIntUnthreaded which returns an event category *other* than wxEVT_CATEGORY_THREAD, and a myEventClassIntThreaded class that returns wxEVT_CATEGORY_THREAD?

Or, is that just a waste of time, and should myEvenClassInt just always return wxEVT_CATEGORY_THREAD, and should I blindly emit this class regardless of whether I'm in the main (GUI) thread or another thread?

catalin
Moderator
Moderator
Posts: 1529
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Any times to avoid using wxThreadEvent, or custom event class returning wxEVT_CATEGORY_THREAD from GetEventCategory(

Post by catalin » Thu Apr 30, 2015 8:06 am

A wxThreadEvent implements deep copying of its string member i.e. in Clone().
That being said, you can use wxThreadEvent everywhere, but for the sake of avoiding confusion I would only use them when emitted from a secondary thread.
Why would you care what GetEventCategory() returns? I reckon you should use the most intuitive events for your need, and only try to change that when the initial choices are no longer up for it.

darlingm
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Apr 11, 2015 7:27 pm

Re: Any times to avoid using wxThreadEvent, or custom event class returning wxEVT_CATEGORY_THREAD from GetEventCategory(

Post by darlingm » Fri May 01, 2015 12:00 am

catalin wrote:A wxThreadEvent implements deep copying of its string member i.e. in Clone().
That being said, you can use wxThreadEvent everywhere, but for the sake of avoiding confusion I would only use them when emitted from a secondary thread.
Why would you care what GetEventCategory() returns? I reckon you should use the most intuitive events for your need, and only try to change that when the initial choices are no longer up for it.
I thought when I had thread involved and was writing custom event classes, that I'd have to care what GetEventCategory() returns, since it appears to cause re-entrancy problems.

I don't like having variables go unused, so I don't like the given event having an int, string, and a long. I prefer to have a class that just has the variable(s) I need. This way, it's less likely code misses something it's supposed to use someday.

I was thinking I'd need a set of event classes that were for being emitted in the main (gui) thread, and an identical (except returned wxEVT_CATEGORY_THREAD) set that were for being emitted in other threads.

But, I was wondering if that was going too far, and if I may as well just create one set of event classes, all of which returned wxEVT_CATEGORY_THREAD.

But, I wasn't sure if returning wxEVT_CATEGORY_THREAD from a custom event class emitted in the main (gui) thread was a bad thing. I don't know exactly what YieldFor() is used for, why you sometimes want it and sometimes don't, etc.

User avatar
doublemax
Moderator
Moderator
Posts: 14509
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Any times to avoid using wxThreadEvent, or custom event class returning wxEVT_CATEGORY_THREAD from GetEventCategory(

Post by doublemax » Fri May 01, 2015 1:02 am

I believe you're thinking too much into this. I'v been using wxWidgets for more than 10 years on a professional level and never had to deal with event categories. And i use multiple threads in most of my applications.

If you really want more background information, I think this is the discussion that consequently lead to the introduction of the event categories:
https://groups.google.com/forum/#!msg/w ... ix5Wq4u7gJ
Use the source, Luke!

Post Reply