GUI not updating when there is a reiterative calculation

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
spamiam
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Wed Apr 22, 2009 1:40 am

GUI not updating when there is a reiterative calculation

Post by spamiam »

I have a simple program that does an iterative calculation and is supposed to send text of values from each iteration to a rich text control. To keep it simple for a test case, I just send some characters without any actual calculation.

I used wxDev C++ to do the preliminary simple layout.

There is the main app called "LayoutFrmApp" in its OnInit() function the frame gets created. The name of the frame is "LayoutFrm".

In LayoutFrm.cpp, I create a wxBoxSizer with a wxRichTextCtrl inside it.

Then after this I call a function that simply writes a few characters to the wxRichTextCtrl repetitively in an infinite while(true) loop. I never see ANYTHING on the screen. But if I loop a few times instead of endlessly, then I do get the program displaying properly once the loop is completed.

So, I thought that maybe I am not yielding control back to the main app while inside the endless loop, so I inserted the line "wxGetApp().Yield();" each time after writing some characters to the text control inside the loop. This still did not allow anything to appear.

So, what should I do? Is there a more effective way to Yield control? Is there a different way to handle the program flow while still effectively getting an infinite (or no pre-determined endpoint) loop

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

Re: GUI not updating when there is a reiterative calculation

Post by doublemax »

Then after this I call a function that simply writes a few characters to the wxRichTextCtrl repetitively in an infinite while(true) loop
From where do you call this function? From an event handler or from wxApp::OnInit() during initialization? In the latter case, the event loop is not running yet, so you won't see any update.
...so I inserted the line "wxGetApp().Yield()
That definitely should have worked.
Is there a different way to handle the program flow while still effectively getting an infinite (or no pre-determined endpoint) loop
The cleanest way is to run the code from a secondary thread. But as you can't access GUI elements from a secondary thread, it requires some more coding effort depending on what you actuzally need.
Use the source, Luke!
spamiam
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Wed Apr 22, 2009 1:40 am

Re: GUI not updating when there is a reiterative calculation

Post by spamiam »

doublemax wrote:From where do you call this function? From an event handler or from wxApp::OnInit() during initialization? In the latter case, the event loop is not running yet, so you won't see any update.
Yes, the function is called when the frame is created in the wxApp::OnInit(). Not in an event handler, as this isn't really an "event-driven" process. The program is supposed to run, do some iterative calculations, display the iteration values (eventually is will be a graphical display, but now it is just text), then stop when it reaches a point where the program determines it is time to stop looping.

So, there will be no user intervention needed. Though I realize that internally there are lots of events happening behind the scenes. I just have no plan on using any of the events myself.

Where should I put the calculation? In the main wxApp, But not in the OnInit()? I can do that, I think. I just have to figure out where to put it. It is not clear to me WHERE to put it so that the event loop can be running at the time.

-Tony
spamiam
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Wed Apr 22, 2009 1:40 am

Re: GUI not updating when there is a reiterative calculation

Post by spamiam »

Maybe I have a good answer. I made the display function an "idle event".

Code: Select all

void LayoutFrm::Display_Positions(wxIdleEvent& evt)
{
    TextPanel->AppendText(_("    this is a test"));
    evt.RequestMore();
}
I found an example of something generally similar. They used the RequestMore() function. Does this increase the frequency of the idle event getting called?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: GUI not updating when there is a reiterative calculation

Post by doublemax »

Code: Select all

They used the RequestMore() function. Does this increase the frequency of the idle event getting called?
This guarantees that you get another idle event as soon as possible. Without it, an idle event is only generated after any other event. So e.g. if the user didn't move the mouse or did anything else that causes an event, you wouldn't get an idle event.
Use the source, Luke!
spamiam
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Wed Apr 22, 2009 1:40 am

Re: GUI not updating when there is a reiterative calculation

Post by spamiam »

Is using the idle event going to get me the highest frequency of reiterations without interfering with the GUI itself?
I was thinking of using the wxApp::OnRun or some other similar wxApp function where I might be able to run a foreground task continuously with whatever multitasking interruptions that are required happening in the background as needed, but without interference by the foreground task

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

Re: GUI not updating when there is a reiterative calculation

Post by doublemax »

Is using the idle event going to get me the highest frequency of reiterations without interfering with the GUI itself?
It always depends on how much work you do in the event handler. As long as the event handler is running, the gui is blocked.

You could also try your first approach, but instead of calling the code from wxApp::OnInit(), override wxAppConsole::OnEventLoopEnter(). You should have a running event loop and wxApp::Yield() should give the expected result.

http://docs.wxwidgets.org/trunk/classwx ... 17e53ae999
Use the source, Luke!
spamiam
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Wed Apr 22, 2009 1:40 am

Re: GUI not updating when there is a reiterative calculation

Post by spamiam »

Thanks, I will try it both ways. Any individual iteration does not take long at all, instead it is likely to simply require lots of iterations. It is OK to take a few seconds as the graphic display may be interesting and informative as the iterations progress.

-Tony
Post Reply