GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Dec 18, 2014 9:00 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Updating a progress bar while calculations are being done
PostPosted: Thu Jun 14, 2007 8:40 am 
Offline
Familiar Face

Joined: Sat Feb 24, 2007 2:52 pm
Posts: 13
I want to create a GUI for a program which performs lengthy calculations. Its current command-line version prints from time to time a percentage indicating the progress of the calculations. I would like the GUI version to show a progress bar (or anything similar) for this same purpose. However, this doesn't seem to be as simple as it sounds at first. AFAIK there would be two options for this:

1) Have the program "refresh" the application window from time to time, using the current progress value. I actually don't know how to do this (or if it's at all possible with GTK+). If I'm not mistaken, by default GTK+ can refresh the application window only when it gets control. As long as some function is doing calculations (basically in a busy loop) GTK has no control and thus the window does not get refreshed in any way.

2) Use multithreading: The main GUI application (and thus GTK) runs in one thread and the calculation routine in another. The GUI thread reads from time to time the progress of the calculation thread and updates the window accordingly.

Either option would be fine, although option 2 would obviously be much better: The application window would be more responsive and it would be easy to implement extra GUI features such as a button to cancel the calculations. However, since I don't have too much experience about creating threads I'm not completely sure how this should be done. And is GTK+ thread-safe?

How should I do this?

I'm using linux and GTK+ 2.6.4.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 15, 2007 9:00 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
With GTK+, you should only ever have one thread touching the GUI at a time. If not, you can run into terrible problems.

One option would be to use a timeout, which would call a function every second or two to update the progress bar.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 16, 2007 10:44 am 
Offline
Familiar Face

Joined: Sat Feb 24, 2007 2:52 pm
Posts: 13
openldev wrote:
With GTK+, you should only ever have one thread touching the GUI at a time. If not, you can run into terrible problems.


As I said, I just want the calculation routine running in one thread and the GUI running in another, updating itself.

Quote:
One option would be to use a timeout, which would call a function every second or two to update the progress bar.


But how do I create threads in gtk?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 17, 2007 12:55 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Check out the sections on:

Threads: http://developer.gnome.org/doc/API/2.0/ ... reads.html
Thread Pools: http://developer.gnome.org/doc/API/2.0/ ... Pools.html
Asynchronous Queues: http://developer.gnome.org/doc/API/2.0/ ... ueues.html

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 24, 2007 12:06 am 
Offline

Joined: Sun Jun 24, 2007 12:01 am
Posts: 1
openldev wrote:
One option would be to use a timeout, which would call a function every second or two to update the progress bar.


I wanted to do something like this some days ago. I first wanted to do it with a timeout, which doesn't refresh the GUI at all.

The timeout function is not called during the operation. It is only called when the operation end.

Which is totally normal.

How can you update a progress bar with a timeout if you are doing something else during this time, without using multi-thread? I don't get it.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 24, 2007 12:24 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
When you create a timeout, you can set the priority. If tasks with a higher priority are pending, the timeout will not be called. So, you should set it high enough to force it to run.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 11:13 am 
Offline
Familiar Face

Joined: Sat Feb 24, 2007 2:52 pm
Posts: 13
It was quite simple, after all:

1) At the beginning of main(), call g_thread_init(NULL);
2) When I want to launch the calculation thread, I call g_thread_create().
3) After launching that thread, set a timer for calling the progress bar update with gtk_timeout_add()
4) Inside that progress bar updating function, if the calculation thread is still running, make the same gtk_timeout_add() again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group