GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Updating a progress bar while calculations are being done

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Warp
Familiar Face


Joined: 24 Feb 2007
Posts: 13

PostPosted: Thu Jun 14, 2007 8:40 am    Post subject: Updating a progress bar while calculations are being done Reply with quote

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.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Fri Jun 15, 2007 9:00 pm    Post subject: Reply with quote

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.
Back to top
Warp
Familiar Face


Joined: 24 Feb 2007
Posts: 13

PostPosted: Sat Jun 16, 2007 10:44 am    Post subject: Reply with quote

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?
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Sun Jun 17, 2007 12:55 pm    Post subject: Reply with quote

Check out the sections on:

Threads: http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html
Thread Pools: http://developer.gnome.org/doc/API/2.0/glib/glib-Thread-Pools.html
Asynchronous Queues: http://developer.gnome.org/doc/API/2.0/glib/glib-Asynchronous-Queues.html
Back to top
bobyjoe



Joined: 24 Jun 2007
Posts: 1

PostPosted: Sun Jun 24, 2007 12:06 am    Post subject: Reply with quote

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.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 387
Location: Fairfax, Virginia

PostPosted: Sun Jun 24, 2007 12:24 am    Post subject: Reply with quote

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.
Back to top
Warp
Familiar Face


Joined: 24 Feb 2007
Posts: 13

PostPosted: Wed Jul 18, 2007 11:13 am    Post subject: Reply with quote

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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP