how to create a window in thread



Here is a small program which does not work on win32 with gtk 2.2.4
The goal is to create a window from a thread.
The window is created but it disappears immediately with the thread.

If keep the thread alive (uncomment the sleep(10000)), the window does not refresh.

------------test.c---------------------
#include <gtk/gtk.h>

GtkWidget *window;
GtkWidget *label;
GThread *thread;

gint function() {
  gdk_threads_enter();  
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  label=gtk_label_new("my text");
  gtk_container_add(GTK_CONTAINER(window), label);
  gtk_widget_realize(window);
  gtk_widget_show_all(window);
  gdk_threads_leave();
  //sleep(10000);
}
gint main(int argc, char *argv[]) {
  g_thread_init (NULL);
  gdk_threads_init();
  gtk_init(&argc,&argv);
  thread = g_thread_create((GThreadFunc)function, NULL, TRUE, NULL);
  g_thread_join(thread);
  gtk_widget_show_all(window);
  gdk_threads_enter();  
  gtk_main();
  gdk_threads_leave();
}
---------------------------------------------
compiled on MinGW with:
gcc -g -DDEBUG -mms-bitfields -mwindows `pkg-config --cflags gtk+-2.0` -c test.c -o test.od
gcc -static -mms-bitfields -mwindows -o test.exe test.od `pkg-config --libs g
tk+-2.0 --libs gthread-2.0` -L/lib
---------------------------------------------

Does anybody have a solution ?



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