Re: gtk2 multithreaded programming win32 issue



>> 2008/8/28 Tor Lillqvist <tml iki fi>:
>>> Have the idle callback function return FALSE... Schedule it again with
>>> g_idle_add() whenever you have something new that needs to be done in
>>> the GUI thread.

First, thanks for you guys here, I had successfully porting my Gtk2 app. from
Linux to Win32 :-)  However, I found a behavior of Linux and Win32 is
quite different ...
Let me explain what I'm doing:

hello button ---> disable itself and create a thread to run non-gui job.
        ---> after non-gui job done, use g_add_idle() to enable hello
button again.

What's odd is when I click on the button, it works OK at the first time.
But when I click on it again ... it stop receive my mouse event !!!!

If I move my mouse away from the button then move it back and click on it again,
it works again.

I did not see this strange behavior under Linux.  I can keep my mouse on top of
the button, just click on it again and again ... all clicks work perfectly.

The source code is here (hopefully, not too long), could someone tell me what's
wrong in my code ??
My Win32 platform is WinXP/SP3, and I'm using gtk+-bundle-2.12.11.zip which I
downloaded from http://www.gtk.org/download-windows.html.

Thanks a lot
KC

------------------------------------------------------------------
/* button2.c */

#include <stdlib.h>
#include <gtk/gtk.h>

gpointer non_gui_worker(gpointer data);
gboolean non_gui_idle(gpointer data);
void gui_cb_clicked(GtkWidget *widget, gpointer data);

gboolean non_gui_idle(gpointer data)
{
    GtkWidget *widget = data;
    gtk_widget_set_sensitive(widget, TRUE);
    return FALSE;
}

gpointer non_gui_worker(gpointer data)
{
    printf("DO SOMETHING IN NON-GUI THREAD ...\n");
    g_idle_add(non_gui_idle, data);
    return NULL;
}

void gui_cb_clicked(GtkWidget *widget, gpointer data)
{
    gtk_widget_set_sensitive(widget, FALSE);
    g_thread_create(non_gui_worker, widget, FALSE, NULL);
}

int main (int argc, char *argv[])
{
    GtkWidget *win    = NULL;
    GtkWidget *button = NULL;

    g_thread_init(NULL);
    gdk_threads_init();
    gtk_init(&argc, &argv);

    gdk_threads_enter();
    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width(GTK_CONTAINER(win), 10);

    g_signal_connect(G_OBJECT(win), "destroy",
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_realize(win);
    button = gtk_button_new_with_label("Hello Gtk+-2.0");

    g_signal_connect(G_OBJECT(button), "clicked",
        G_CALLBACK(gui_cb_clicked), (gpointer) "Data Passed");

    gtk_container_add(GTK_CONTAINER(win), button);

    gtk_widget_show_all(win);
    gtk_main();
    gdk_threads_leave();
    exit(0);
}


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