Is there a way to handle multiple events at the same time? 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
HansLjy
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri May 21, 2021 6:44 am

Is there a way to handle multiple events at the same time?

Post by HansLjy »

Say I have an application which I would like to display a count down on it, at the same time I want to execute some other codes. In this case, I notice that the OnTimer handler won't work correctly. (I may be wrong but I think it is ridiculous for the count down to stop when I am busy handling a button click)So how can I fix this ?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Is there a way to handle multiple events at the same time?

Post by doublemax »

The only workaround would be to call ::wxYield() regularly in the button event handler, so that other events can get processed.

Or you need to move the long-running code to a separate thread.
Use the source, Luke!
HansLjy
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri May 21, 2021 6:44 am

Re: Is there a way to handle multiple events at the same time?

Post by HansLjy »

doublemax wrote: Fri Jun 25, 2021 10:35 am The only workaround would be to call ::wxYield() regularly in the button event handler, so that other events can get processed.

Or you need to move the long-running code to a separate thread.
Thanks. Speak of multi-threading, I want to know what will happen if I try to call the render function in another thread when wxwidgets is already busy rendering? For example, I want to wait until a package is received from the net and then display something. How can I do this? Can I call SetLabelText directly in this thread? Or there are other ways?
Last edited by HansLjy on Fri Jun 25, 2021 12:29 pm, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Is there a way to handle multiple events at the same time?

Post by doublemax »

GUI operations must only happen in the main thread.
Use the source, Luke!
HansLjy
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri May 21, 2021 6:44 am

Re: Is there a way to handle multiple events at the same time?

Post by HansLjy »

doublemax wrote: Fri Jun 25, 2021 12:29 pm GUI operations must only happen in the main thread.
I see. But I was wondering how this could be implemented: I want to wait for a signal from external source and display a text after that (at the same time I want the count down to continue). How can I tell wxWidgets it's time to refresh?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Is there a way to handle multiple events at the same time?

Post by doublemax »

Can I call SetLabelText directly in this thread?
No.

There are three main ways to handle this situation.

1) Send an event to the main thread

2) Use wxMessageQueue to communicate with the main thread (you'll need a timer event that regularly checks the queue, or combine it with 1) ) https://docs.wxwidgets.org/trunk/classw ... _01_4.html

3) Use wxEvtHandler::CallAfter() to call some code asynchronously in the main thread. (I would use this only for small code snippets ) https://docs.wxwidgets.org/trunk/classw ... 50b3719519
Use the source, Luke!
HansLjy
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri May 21, 2021 6:44 am

Re: Is there a way to handle multiple events at the same time?

Post by HansLjy »

doublemax wrote: Fri Jun 25, 2021 12:39 pm 1) Send an event to the main thread
Seems this is what I want, is there some kind of tutorial about this? What part of the online document of wxWidgets cover this?
Post Reply