Modal dialog and wxQueueEvent

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.
ursatz
Earned a small fee
Earned a small fee
Posts: 11
Joined: Sat May 12, 2018 1:00 am

Modal dialog and wxQueueEvent

Postby ursatz » Mon Jun 11, 2018 4:01 pm

Hello,
I have an app that does some heavy image processing, sometimes using all available cores. The processing thread sends out periodic progress messages, and I intercept those and use wxQueueEvent to put them on the GUI thread, which then updates a gauge control. I have a couple of wxFrames that use this technique with no problem, no matter how heavy the cpu load.

I'm now trying to use this technique in a modal dialog, where the user selects a number of files for processing. I start a worker thread running prior to the call to ShowModal(), and when the user selects the files, I pass them into the thread class and tell it to start processing. (I'm following the model in the last post here: https://groups.google.com/forum/#!topic ... AtQZPLn8x0). In addition to updating the progress bar, I also have a text box that logs what's happening, and I use wxQueueEvent to pass strings along to be appended to this text box from the worker thread. This all works great if the processing is not too heavy. But when a file is encountered that requires more cpu, suddenly the app stops pulling messages off the queue - the progress bar and the text box are no longer updated, even though wxQueueEvent is still being called. When this happens, the UI can remain stuck until all processing stops, at which point all the messages get handled at once. The progress bar updates multiple times, and the text box fills up with log messages. Strangely, when the program gets into this blocked state, moving the mouse causes it to get unblocked! Something about a mouse event causes the message queue to be processed.

As I said, there is no such problem with my wxFrames - they use wxQueueEvent for updating a progress bar, and there is no issue with this even under the heaviest cpu load. But I can't get this to work properly in a modal dialog. Does anyone know of a reason why this might be happening? Specifically 1) why it's different with a modal dialog as opposed to a wxFrame, 2) why a mouse event would cause the app to get un-stuck?

Thanks for any help,
Bill

ursatz
Earned a small fee
Earned a small fee
Posts: 11
Joined: Sat May 12, 2018 1:00 am

Re: Modal dialog and wxQueueEvent

Postby ursatz » Mon Jun 11, 2018 4:17 pm

...wxWidgets 3.1.1, Windows 10.

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

Re: Modal dialog and wxQueueEvent

Postby doublemax » Mon Jun 11, 2018 4:34 pm

I don't know why this happens, i can only speculate. Modal dialogs have their own event loop. I assume there is still a (parent) wxFrame around somewhere? Then you'll have two running event loops. Also, wxQueueEvent calls WakeUpIdle() to wake up the event loop and make sure the new event gets processed. It's possible there is a bug and it wakes up the wrong event loop.

However, regardless of all that, i have a suspicion that you're generating too many events too quickly. Can you give some numbers?

For GUI updates i usually don't use events. As 10-20 GUI updates per second, is normally enough, i just use a timer and poll the status changes and update my GUI. If that doesn't require a major redesign for you, you should try that.

If you want more insight on what's going on, try asking on the wx-users group/mailing list https://groups.google.com/forum/#!forum/wx-users where you can reach the core developers. This here is a user forum.
Use the source, Luke!

ursatz
Earned a small fee
Earned a small fee
Posts: 11
Joined: Sat May 12, 2018 1:00 am

Re: Modal dialog and wxQueueEvent

Postby ursatz » Mon Jun 11, 2018 4:54 pm

I think you're right, it must be too many events too quickly. With the more complex processing, there can be several hundred updates per second at certain points. I tried throttling the event queueing back by a factor of 10, and it helped a bit, but the problem was still there.

Good idea about using a timer. I'll give that a try.

ursatz
Earned a small fee
Earned a small fee
Posts: 11
Joined: Sat May 12, 2018 1:00 am

Re: Modal dialog and wxQueueEvent

Postby ursatz » Mon Jun 11, 2018 6:30 pm

The timer approach works great. I guess the combination of modal dialog + hundreds (or thousands) of events per second makes the message queue unhappy.

Thanks,
Bill


Return to “C++ Development”

Who is online

Users browsing this forum: Baidu [Spider] and 19 guests