Threads & popup windows under mswin



Hi all,

The following code is an example of what I want to do: when clicking the 
button "test" a new thread is created and inside this thread a popup window 
is shown. This works fine under linux but the program freezes under windows. 
Is this the correct way to do this?

I am using gtk+ libraries from dropline (http://www.dropline.net/gtk) , and 
also tried the runtime binaries available at gimp.org.

Code:

#include <gtk/gtk.h>

/* Dialog message */
void msg(GtkWidget *window){
        GtkWidget *dialog;
        
        gdk_threads_enter();
        dialog = gtk_message_dialog_new (GTK_WINDOW(window),
                        GTK_DIALOG_DESTROY_WITH_PARENT,
                        GTK_MESSAGE_INFO,
                        GTK_BUTTONS_OK,
                        "OK");
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
        gdk_threads_leave();
}

/* test thread */
void *test(void *data){

        /* Do something ... */
        
        /* Shows final info */
    msg(data);
        
    return(NULL);
}

/* test callback */
void test_callback(GtkButton *button, GtkWidget *window){
    g_thread_create(test, window, FALSE, NULL);
}

/* window with 2 buttons */
GtkWidget *create_window(){
    GtkWidget *window, *button1, *button2, *hbuttonbox;
    
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);   
    g_signal_connect(GTK_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), 
window);
    
    hbuttonbox = gtk_hbutton_box_new();
    gtk_widget_show(hbuttonbox);
    gtk_container_add (GTK_CONTAINER (window), hbuttonbox);
    
    button1 = gtk_button_new_from_stock ("gtk-quit");
        gtk_widget_show (button1);
        gtk_container_add (GTK_CONTAINER (hbuttonbox), button1);
        g_signal_connect(GTK_OBJECT(button1), "clicked", G_CALLBACK(gtk_main_quit), 
NULL);
        
        button2 = gtk_button_new_with_label("test");
        gtk_widget_show(button2);
        gtk_container_add(GTK_CONTAINER(hbuttonbox), button2);
        g_signal_connect(GTK_OBJECT(button2), "clicked", G_CALLBACK(test_callback), 
window);
        
        return(window);
}

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

        /* Inicia o gtk*/
        g_thread_init(NULL);
        gdk_threads_init();
        gtk_init (&argc, &argv);
        
        window = create_window();
        
        gtk_widget_show(window);
        
        gdk_threads_enter();
        gtk_main ();
        gdk_threads_leave();

    return 0;
}


-- 
Miguel Figueiredo
IT student / Marine Biologist
"Tem calma irmão que a morte não precisa do teu sim, é coisa certa, mais vale 
fazer da vida um festim." ALC




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