Re: Updating progress bar



Hi,

Federico Bravo <fedebravo iol it> writes:

> Hello. Sorry for my last posting (the answer was right in the previous
> post on the list, but I sent mine before reading it).  I'd like to
> update a progress bar while a time-consuming function is working.  I
> read in the FAQ that I have to call  'while(g_main_iteration(FALSE));'
> inside the function that changes the widget. Now, that function in my
> case is as follows :
> 
> gint display_progress_update (gpointer p)
> {
> static float percentage=0.0;
> gtk_progress_bar_update(GTK_PROGRESS_BAR(p), percentage);
> percentage+=0.1;
> if (percentage>=1.0)
> percentage=0.0;
> return (TRUE);
> }
> 
> and the calling function:
> 
> progress=gtk_timeout_add(50,display_progress_update,p);
> for(;;);  /* this represents a time consuming function.
> 
> 
> My problem is that if I insert the 'while' snippet into
> display_progress_update (that is after gtk_progress_bar_update)  it
> simply does not work, while if I put it inside the for loop it works
> well. I do not want to insert it in my time consuming function since it
> could be just reading a long file, with no loops (that's why I want to
> use timeouts ). I even thought of setting up another timeout just for
> it, but still no effect.

first you need to understand how the main loop works; I'll try to give
you some hints. When your time-consuming function is called from the
main loop, it runs and nothing else can run at the same time. So if your
function blocks, it blocks the main loop. Later when your time-consuming
function finishes and gives control back to the main loop, the latter 
checks if any timeouts have been reached in the meantime and call them. 
That's why using a timeout for your progress bar won't work unless you 
explicitely drive the main loop from the time consuming function by 
calling g_main_iteration(). 

There are other ways to make this work. You could for example split up 
your time-consuming function in smaller pieces that are guaranteed to 
finish in a reasonable time, then do all the time-consuming work by 
calling those smaller functions when the main loop is idle. This way you 
can achieve having a snappy respsonible GUI while the heavy work is done 
(and you can make it possible to let the user cancel the calculations). 
If there is the risk that your time-consuming function may block and 
there's no way to convert this to a non-blocking function, you should
consider using a seperate thread for this.


Salut, Sven




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]