LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2006, 08:15 AM   #1
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Rep: Reputation: 15
Post [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"));
   [...]
}
 
Old 10-24-2006, 08:23 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 10-24-2006 at 08:29 AM.
 
Old 10-24-2006, 08:28 AM   #3
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
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)
 
Old 10-24-2006, 08:32 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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
 
Old 10-24-2006, 09:09 AM   #5
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
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 :-(( )
 
Old 10-24-2006, 09:11 AM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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..
 
Old 10-24-2006, 09:31 AM   #7
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
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
 
Old 10-24-2006, 10:22 AM   #8
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
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.
 
Old 10-24-2006, 11:24 AM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 10-24-2006 at 11:41 AM.
 
Old 10-24-2006, 11:45 AM   #10
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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.
 
Old 10-24-2006, 02:57 PM   #11
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
thanks all.. I have found the book (in PDF) and I am going to learn threads..
 
  


Reply

Tags
events, gui, wxwidgets



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nagios Event Handler not running - NRPE: Unable to read output notque Linux - Software 7 04-05-2013 06:27 AM
LXer: Book Review: Cross platform GUI programming with wxWidgets LXer Syndicated Linux News 2 12-21-2005 12:52 PM
Mandrake 10.1 is running but no gui??? Bill Johns Linux - Software 8 04-13-2005 05:07 AM
Help me get my GUI running BuzzStPoint Mandriva 6 06-09-2004 09:32 PM
running remote gui programs w/ X steyr Slackware 7 01-11-2004 07:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration