RE: gtk progress bar and gdk_flush()



Even Gtk is event driven, the gdk_flush should send all the output to
the screen. The microsoft windows progress bar can update screen without
go back to main loop.

Think about you want to load 100M data from a file and encrypt it and
write back to a new file, maybe you want to every 10 M, you want to
update progress bar. It is easy just write one function and wait for it
returns. All the variable for encryption and buffers could be local for
this function. 

For timeout event driven, Slice the task into small pices and in every
timeout function load one pice is hard to control.

Thanks.




-----Original Message-----
From: gtk-app-devel-list-admin gnome org
[mailto:gtk-app-devel-list-admin gnome org] On Behalf Of Roland Smith
Sent: Saturday, October 25, 2003 11:06 AM
To: gtk-app-devel-list gnome org
Subject: Re: gtk progress bar and gdk_flush()

--Signature=_Sat__25_Oct_2003_17_06_21_+0200_kW0FdRG1BvR=U2_K
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

On Fri, 24 Oct 2003 12:01:27 -0400
"Tom Liu" <tom liu flextrade com> wrote:

Hi,

I am working on the progress bar, I can't my progress bar updated in a
loop:
Even the gdk_flush() can't make it update on screen.

You don't give GTK+ the opportunity to update the screen. Remember GTK+
is event
driven. Add the following code to your loop, and it should work.
 
int done;
GtkWidget pg;
char msg[100];

for (done=0;done<100;done+=20){
      sprintf (msg,"%d was done" , done);
      gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pg),msg);

gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pg),(double)(done)/100);
        /* Add the following code */
        while (gtk_events_pending ()) {
                gtk_main_iteration ();
        }
      gdk_flush();
        /* remove the sleep call, it will block your application. */
      /* sleep(1); */ // do something take a long time.
}

A better strategy is to do the work (and update the progress bar) in a
timeout
function, with gtk_timeout_add(). Note that this timeout function should
only do
a small bit of work. It will be called repeatedly, so you can do part of
your
work every time the timeout is called by GTK+.

Think of timeouts as loops with GTK+ assistance.

Roland
-- 
R.F. Smith                           /"\    ASCII Ribbon Campaign
r s m i t h @ x s 4 a l l . n l      \ /    No HTML/RTF in email
http://www.xs4all.nl/~rsmith/         X     No Word docs in email
                                     / \    Respect for open standards

--Signature=_Sat__25_Oct_2003_17_06_21_+0200_kW0FdRG1BvR=U2_K
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/mpFyEnfvsMMhpyURAsrwAJ0XAciIKYHHil1ybJ58iS2TAeuGFgCgofD/
L3lIt2KJUl/Ars16f1zFogg=
=tBXC
-----END PGP SIGNATURE-----

--Signature=_Sat__25_Oct_2003_17_06_21_+0200_kW0FdRG1BvR=U2_K--




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