Adding the work inside the idle function would work, but would be hard
to implement (state is spread out between function calls, so hard to
debug) and not cross-platform, since I think Linux has only recently
added support for AIO (asynchronous input/output). If you'd rather use
threads, here's a small sample that just updates the progress bar; you
would do the rest of the work (such as reading bits of the file)
inside the thread.

#include <gtk/gtk.h>

GtkWidget *bar;

/**
 * this function will be run in the *main thread*, i.e.
 * from where gtk_main is running
 */

gboolean
update_callback (gpointer data)
{
        gdouble val;

        val = (gdouble) GPOINTER_TO_INT (data) / 1000;

        /* these are needed, read the documentation on gdk_threads_enter */
        gdk_threads_enter ();
        gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), val);
        gdk_threads_leave ();

        return FALSE;
}

gpointer
file_thread (gpointer data)
{
        int    i;
        double val;

        for (i = 0; i < 1000; i++) {
                /* add the idle function. this works from any thread */
                g_idle_add (update_callback, GINT_TO_POINTER (i));
                g_usleep (5000);
        }

        gtk_main_quit ();
}

int
main (int argc, char *argv[])
{
        GtkWidget *window;

        gtk_init (&argc, &argv);

        g_thread_init (NULL);

        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

        bar = gtk_progress_bar_new ();

        gtk_container_add (GTK_CONTAINER (window), bar);

        gtk_widget_show_all (window);

        g_thread_create (file_thread, NULL, TRUE, NULL);

        gtk_main ();
}



Hope that helps!

Samuel Cormier-Iijima

On 11/5/06, Van H Tran <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> --- Kim Jongha <[EMAIL PROTECTED]> wrote:
>
> > /* and the for loop */
> >  double val;
> >  for(i = 0; i< SOME_NUMBER; i++)
> >  {
> >    val = (100.0 /SOME_NUMBER)* i /100.0;
> >    g_idle_add(show_bar_idle_cb, INT_TO_POINT(val));
> >  **SOMEWORK**
> >  }
> >
> > The problem is that timeout events don't occured
> > before finishing
> > SOMEWORK(read file with fscanf)
>
> How about adding SOMEWORK inside the g_idle_callback
> function?
>
> Cheers,
> TranVan Hoang
> >
> > --------------
> >  double val = 0;
> >    g_idle_add(show_bar_idle_cb, INT_TO_POINT(val));
> >  for(i = 0; i< SOME_NUMBER; i++)
> >  {
> >    val = (100.0 /SOME_NUMBER)* i /100.0;
> >  **SOMEWORK**
> >  }
> > --------------
> > upper codes don't work too.
> >
> > 2006/11/5, Kim Jongha <[EMAIL PROTECTED]>:
> > > 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.
> > >
> > _______________________________________________
> > gtk-app-devel-list mailing list
> > gtk-app-devel-list@gnome.org
> >
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
> >
>
> _______________________________________________
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to