Re: Problem with GtkProgressBar and multi thread application



On Sun, 2006-12-24 at 00:26 +0100, Tomasz Jankowski wrote:
Hello!

This is simple code:
#include <gtk/gtk.h>

gboolean pulse_it (gpointer data)   {
    gtk_progress_bar_pulse (data);
    return TRUE;
}

gint thread_fun (gpointer data) {
    gint source_id = g_timeout_add (100, (GSourceFunc) pulse_it, data);

    g_usleep (5000000);

    g_source_remove (source_id);

    gdk_threads_enter ();
    gtk_progress_bar_set_text (data, "lol!");
    gdk_threads_leave ();
}

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

    gtk_init (&argc, &argv);

    g_thread_init (NULL);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    pbar = gtk_progress_bar_new ();

    gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (pbar));

    gtk_widget_show_all (window);

    g_thread_create (thread_fun, pbar, FALSE, NULL);

    gdk_threads_enter ();
    gtk_main ();
    gdk_threads_leave ();

    return 0;
}


It doesn't work form me. Progress bar is pulsing, but when event source is
removed something weird starts happening. Whole application still works, but
GUI doesn't update (new progress bar's text doesn't display). Any idea how
to solve this?! Maybe I'm too stupid...

I use Ubuntu with GTK+v.2.10.6, GLib 2.12.4 and GCC 4.1


I'm no expert in either gtk or threads, but this seems to be the most
usual way to solve your problem:

*change your function so it doesn't require to use gdk_threads_...:


gint thread_fun (gpointer data) {
    gint source_id = g_timeout_add (100, (GSourceFunc) pulse_it, data);

    g_usleep (5000000);

    g_source_remove (source_id);
    
   source_id = g_timeout_add(0, (GSourceFunc) set_lol_text, data);

   // gdk_threads_enter ();
    //gtk_progress_bar_set_text (data, "lol!");
    //gdk_threads_leave ();
}

*add a new function to change the text. This function returns FALSE
indicating that it must be called only once:

gboolean set_lol_text (gpointer data)   {
        gtk_progress_bar_set_text (data, "LOL!");
        return FALSE;
}

I hope this helps.





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