Discussion:
How to response wxCommandEvent asynchronously?
Yili Zhao
2014-02-06 14:20:57 UTC
Permalink
Hi,
I am developing an application with wxWidgets 3.0, and after receive a
menuitem command, I want to do like these:

-----------------------------------------------------------------------------------------------------------------------------------------------------
EVT_MENU(ID_CREATE_ALPHA, MyFrame::OnCreateAlpha)

void MyFrame::OnCreateAlpha(wxCommandEvent &event)
{
do a long time-consumed task;
}
------------------------------------------------------------------------------------------------------------------------------------------------------

The most important thing is that I want to run a long time-consumed task
in event handler "OnCreateAlpha".

In Java/Swing, all UI events should be processed in UI thread or the main
thread, and there is only One UI thread, so it is a single thread model.

How about the wxWidgets UI model? Is it a single thread model like Swing?

And the event handler "OnCreateAlpha" runs in which thread, in UI thread
or in a separate thread?

In order to avoid blocking the user interface, I plan to do like this:

1. execute the "long time-consumed task" in a separate worker thread;

2. In the task running process, I want to report the running progress to
the main frame, so that the main frame can display a progress bar to let
user know the current status;

3. When the task completed, it can notify the main frame, and the main
frame can response to this event (maybe repaint its client area).

How can I implement this asynchronous task model in wxWidgets?

Are there some helper class like "AsyncTask" in Android?

Sample code would be really helpful, and thanks very much!
--
Yili Zhao
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-02-06 15:06:42 UTC
Permalink
On Thu, 6 Feb 2014 22:20:57 +0800 Yili Zhao wrote:

YZ> void MyFrame::OnCreateAlpha(wxCommandEvent &event)
YZ> {
YZ> do a long time-consumed task;
YZ> }

The best way to do this by far is to launch a background thread.

YZ> In Java/Swing, all UI events should be processed in UI thread or the main
YZ> thread, and there is only One UI thread, so it is a single thread model.
YZ>
YZ> How about the wxWidgets UI model? Is it a single thread model like Swing?

Yes.

YZ> And the event handler "OnCreateAlpha" runs in which thread, in UI thread
YZ> or in a separate thread?

UI thread.

YZ> In order to avoid blocking the user interface, I plan to do like this:
YZ>
YZ> 1. execute the "long time-consumed task" in a separate worker thread;
YZ>
YZ> 2. In the task running process, I want to report the running progress to
YZ> the main frame, so that the main frame can display a progress bar to let
YZ> user know the current status;
YZ>
YZ> 3. When the task completed, it can notify the main frame, and the main
YZ> frame can response to this event (maybe repaint its client area).
YZ>
YZ> How can I implement this asynchronous task model in wxWidgets?

Use wxThread to launch the thread (doh), post wxThreadEvent to the main
thread to communicate progress/result to it and maybe use wxMessageQueue to
send commands to the worker thread.

YZ> Sample code would be really helpful, and thanks very much!

The thread sample is a bit too complicated for its own good but it does
exist.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Yili Zhao
2014-02-09 13:14:18 UTC
Permalink
Hi Vadim,
thanks for the help, and my model like:

class MyFrame: public wxFrame
{
public:
TaskThread * task;
};

class TaskThread: public wxThread
{
protected:
virtual ExitCode Entry ();
virtual void OnExit();
};

I have two questions:

1. Why I can not find "OnExit" description at wxThread documentation
"http://docs.wxwidgets.org/stable/classwx_thread.html"?

2. If the TaskThread object "task" running ended, should I delete the
pointer "task" with "delete" keyword?

Thanks!
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-02-09 13:39:34 UTC
Permalink
On Sun, 9 Feb 2014 05:14:18 -0800 (PST) Yili Zhao wrote:

YZ> Hi Vadim,
YZ> thanks for the help, and my model like:
YZ>
YZ> class MyFrame: public wxFrame
YZ> {
YZ> public:
YZ> TaskThread * task;
YZ> };
YZ>
YZ> class TaskThread: public wxThread
YZ> {
YZ> protected:
YZ> virtual ExitCode Entry ();
YZ> virtual void OnExit();
YZ> };
YZ>
YZ> I have two questions:
YZ>
YZ> 1. Why I can not find "OnExit" description at wxThread documentation
YZ> "http://docs.wxwidgets.org/stable/classwx_thread.html"?

This looks like a bug in the documentation generation, this private method
was not appearing in the generated documentation even though it is
generated. I'll fix this, thanks for noticing.

YZ> 2. If the TaskThread object "task" running ended, should I delete the
YZ> pointer "task" with "delete" keyword?

This is explained at http://docs.wxwidgets.org/trunk/classwx_thread.html
however: it depends on how your TaskThread is created, i.e. what kind is
it. If it's a detached thread, it deletes itself on termination.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Loading...