LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [wxWidgets] how to unfroze the GUI while the event is running? (https://www.linuxquestions.org/questions/programming-9/%5Bwxwidgets%5D-how-to-unfroze-the-gui-while-the-event-is-running-495098/)

sylvaticus 10-24-2006 08:15 AM

[wxWidgets] how to unfroze the GUI while the event is running?
 
Hello, I have to make a very simple GUI for my C++ mathematical model.
Basically I just need a wxTextContr window that display the output of my model as it is produced.
The problem is that is is display at the end of the event call (when the model end, sometimes after hours), while I need it in real-time.

I am completelly new to GUI programming, so I think what I wrote is completelly ridicolous...
I would seriously appreciate on GUI programming strategies to improve my small GUI ;-).

This is what I wrote:
Code:

class
regmasapp : public wxApp
{
        public:
                virtual bool OnInit();
};
class
regmasFrame : public wxFrame
{
  public:
      void OnRun( wxCommandEvent& event );
      wxTextCtrl* logAreaBox;
      [...]
}
void
regmasFrame::OnRun( wxCommandEvent& WXUNUSED( event ) )
{
        MainProgram* myProgram;
        myProgram = new MainProgram( this);
        myProgram->run();
}
class MainProgram{
private:
      regmasFrame* GUI;
public:
        MainProgram( regmasFrame* GUI_h);
        ~MainProgram();       
        void run();
};
and finally...
MainProgram::MainProgram(regmasFrame* GUI_h):GUI(GUI_h)
{
}
void
MainProgram::run(){
  [...]
  GUI->logAreaBox->AppendText(wxT("Stampa qualcosa\n"));
  [...]
}


dmail 10-24-2006 08:23 AM

If I understand you correctly then you are computing data which takes a long time; yet you want to display results as they happen rather than at the end of the computation.
Use two threads one for the gui and one for the computation, have data passing from the maths thread into a threadsafe queue and display the results in that thread.

This allows the gui to be responsive whilst still computing the data, tho I would give a higher priority to the the non gui thread.

sylvaticus 10-24-2006 08:28 AM

Quote:

Originally Posted by dmail
If I understand you correctly then you are computing data which takes a long time; yet you want to display results as they happen rather than at the end of the computation.
Use two threads one for the gui and one for the computation, have data passing from the maths thread into a threadsafe queue and display the results in that thread.

yes, that's correct.
Can you raccomand me a tutorial on this point?
(I was thinking that the thread division was just made automatically by wxWidgets when calling events)

dmail 10-24-2006 08:32 AM

My knowledge of wxWidgets is very minimal(almost nothing lol), so you may be correct. If its a thread tutorial you are looking for then I would recommend Posix thread tut

sylvaticus 10-24-2006 09:09 AM

Quote:

Originally Posted by dmail
My knowledge of wxWidgets is very minimal(almost nothing lol), so you may be correct. If its a thread tutorial you are looking for then I would recommend Posix thread tut

thanks.. I really think there is a "wxWay" to use threads in a wxWidget application, but yr generic tutorial is very nice (just too flashing banners on that site :-(( )

xhi 10-24-2006 09:11 AM

wxWidgets has a thread class you can use, wxThread.

iirc there is an entire chapter dedicated to threads in a book that is available for free from the prentice hall website. i cant think of the name of the book though..

sylvaticus 10-24-2006 09:31 AM

Quote:

Originally Posted by xhi
wxWidgets has a thread class you can use, wxThread.

iirc there is an entire chapter dedicated to threads in a book that is available for free from the prentice hall website. i cant think of the name of the book though..

I suppose you mean "Cross-Platform GUI Programming with wxWidgets", but it doesn't look for free:
http://vig.prenhall.com/catalog/acad...473816,00.html

sylvaticus 10-24-2006 10:22 AM

I am really, really lost on all of this.
here I read:
Quote:

Before starting an MT application (or starting to add MT features to an existing one) it is worth asking oneself if there is no easier and safer way to implement the same functionality. Of course, in some situations threads really make sense (classical example is a server application which launches a new thread for each new client), but in others it might be a very poor choice (example: launching a separate thread when doing a long computation to show a progress dialog). Other implementation choices are available: for the progress dialog example it is far better to do the calculations in the idle handler or even simply do everything at once but call wxWindow::Update() periodically to update the screen.
That's more or less my case... a long computation where I need just to display things sequentially. Yes, I would also like to use the capability to pause,resume,stop, but this isn't really so necessary.

dmail 10-24-2006 11:24 AM

What that example does not taken into account is "when the model end, sometimes after hours", it just says long computation and you want it realtime.

Quote:

...it is far better to do the calculations in the idle handler ...
Quote:

http://www.wxwindows.org/manuals/2.6...ml#wxidleevent
This class is used for idle events, which are generated when the system becomes idle...
Which could be hours later.

Quote:

...or even simply do everything at once but call wxWindow::Update() periodically to update the screen.
...which cou...

I personally see this as the correct method to acheive what you want and is the way many guis work. Their comment I would see as a word of warning that everything may not go as you wish, if you don't have an understanding of threads and the benefits they do or do not give.

On the other hand if you do not want to use threads, then you would have to stop the computation at some point to display results and then continue...

If it was to be run on a dual core cpu, I would try splitting the computation into threads:)


[edit]misread the comment about wxWindow::Update().
This is an option.

xhi 10-24-2006 11:45 AM

Quote:

Originally Posted by sylvaticus
I suppose you mean "Cross-Platform GUI Programming with wxWidgets", but it doesn't look for free:
http://vig.prenhall.com/catalog/acad...473816,00.html

yeh it is but not from the normal pren hall website.. look for it on here.

sylvaticus 10-24-2006 02:57 PM

thanks all.. I have found the book (in PDF) and I am going to learn threads..


All times are GMT -5. The time now is 09:15 PM.