Re: Updating (showing) the widgets of a dialog in a g_thread



On Sat, Sep 22, 2007 at 10:49:56PM -0400, Andrew Smith wrote:
I have the following setup:

gpointer thread1(gpointer data)
{
     run_long_function();
     return NULL;
}

gpointer thread2(gpointer data)
{
     GtkWidget* dialog;

     gdk_threads_enter();
         dialog = gtk_message_dialog_new(GTK_WINDOW(win_main),
                   GTK_DIALOG_DESTROY_WITH_PARENT,
                   GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "asd");
         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
         gtk_widget_show(dialog);
     gdk_threads_leave();

     while (thread1 is running)
     {
         gdk_threads_enter();
             //!! I want to make sure the dialog is shown
             // the following does not help
             while (gtk_events_pending())
                 gtk_main_iteration();
         gdk_threads_leave();

         usleep(500000);
     }

     gdk_threads_enter();
         gtk_widget_destroy(dialog);
     gdk_threads_leave();

     return NULL;
}

Scratch all this.

Access the GUI only from the thread running gtk_main() (AKA
main thread).

Never manually serialize Gtk+ main loop iterations with
gtk_main_iteration() (you are writing a *multithreaded*
program, remember).

NEVER do things like

    usleep(500000);

in the main thread.

Just construct and show the dialog and let the Gtk+ main
loop run normally (by quitting the function that constructs
it).

Add

    g_idle_add(long_function_finished, whatever);

to the end of the thread running long_function():
long_function_finished() will be executed in the main loop,
i.e. the main thread.

No locks, no obscure constructs, works on Win32 too.

Yeti

--
http://gwyddion.net/



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