|
|
| Author |
Message |
|
|
Warp Familiar Face
Joined: 24 Feb 2007 Posts: 13
|
Posted: Thu Jun 14, 2007 8:40 am Post subject: Updating a progress bar while calculations are being done |
|
|
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
|
Posted: Fri Jun 15, 2007 9:00 pm Post subject: |
|
|
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
|
Posted: Sat Jun 16, 2007 10:44 am Post subject: |
|
|
| 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
|
|
| Back to top |
|
 |
bobyjoe
Joined: 24 Jun 2007 Posts: 1
|
Posted: Sun Jun 24, 2007 12:06 am Post subject: |
|
|
| 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
|
Posted: Sun Jun 24, 2007 12:24 am Post subject: |
|
|
| 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
|
Posted: Wed Jul 18, 2007 11:13 am Post subject: |
|
|
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 |
|
 |
|