Re: questionfor progress bar



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sun, Nov 05, 2006 at 02:59:30PM +0900, Kim Jongha wrote:
Greeting,

progress bar is updated "only" using timeouts ?

I read some big file and want to show up the progress how much App.
read file. so I use progress bar like below


double val;
for(i = 0; i< SOME_NUMBER; i++)
{
   val = (100.0 /SOME_NUMBER)* i /100.0;
  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pbar), val);
   gtk_widget_show(pbar);
}

No error, 'val' value is greater than 0.0 less than 1.0. but it
doesn't work while app. read file. when app read file completly, then
progress bar filled fully is shown up.
I try to use timeouts, and that how can I read file.. ?

give me a advise, thank you.

Many approaches have been stated. I usually prefer to stuff the file
reading function into the idle handler (well, not the whole thing, of
course, but a function which reads some reasonably-sized chunk). This
way, the GUI stays responsive (the progress bar not moving is probably
just part of the problem), and I don't have to mess with threads. As a
very incomplete (and untested) example:

Slurp a whole file into a GString (caveat emptor: it'll fill your memory
:)

 | GSourceFunc chunk_reader;
 | 
 | typedef struct {
 |   GString *buf;
 |   int fd;
 | } file_reader;
 | 
 | file_reader rd;
 | rd.buf = g_string_new("");
 | rd.fd = -1; /* not open yet */
 | 
 | g_idle_add(chunk_reader, (gpointer) &rd);
 | 
 | /* now go about GUI business, like so */
 | gtk_main();
 | 
 | g_string_free(rd.buf, TRUE);
 | if(rd.fd >= 0) close(rd.fd);
 | 
 | static gboolean chunk_reader(gpointer data)
 | {
 |   char chunk[4096];
 |   size_t len;
 |   file_reader *rd = (file_reader *) data;
 |   if(data->fd < 0) { /* not open yet */
 |     if( (data->fd = open("/path/to/my/file", O_RDONLY)) < 0 ) {
 |       g_warning("Gosh! can't open file!");
 |       return FALSE; /* remove us from idle */
 |     }
 |   }
 |   len = read(rd->fd, chunk, sizeof(chunk));
 |   g_string_append_len(rd->buf, chunk, len);
 |   if(len<sizeof(chunk)) {
 |     close(rd->fd);
 |     rd->fd = -1;
 |     return FALSE; /* we are finished */
 |   }
 |   returnTRUE;
 | }

Now you just have to enhance the struct file_reader by a member counting
the bytes read and maybe a guess of how much to do and update your
progress bar accordingnly (how to find out the total amount is left as
an exercise... and so on and so forth ;)

A pointer to the progress bar in the struct file_reader might come in
handy too, to be able to call the update func from whithin the idle
handler.

HTH
- -- tomÃs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFTeAzBcgs9XrR2kYRAhxtAJ98gP+ZUXsQr3XGGw+rT0NlE6hNfQCfQ6dA
dciNmy1l9tPkrtnJj+5V+SE=
=rKfJ
-----END PGP SIGNATURE-----




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