当我尝试在其他线程中调用GtkWidget对象时,GTK应用程序崩溃

时间:2012-12-06 23:15:41

标签: c multithreading gtk

我的文章输入和按钮非常简单。

当用户按下按钮时,应用程序从URL下载文件(在其他线程中)并在成功时打开对话框消息,所有操作都已完成。在下载过程中我激活微调器(如忙)

enter image description here

由于我不知道连接下载文件需要多长时间,因此我使用单独的线程。但是在“对话节目”中我的应用程序失败了,我得到了错误:

(enter_license.exe:210232): Gdk-WARNING **: gdkdrawable-win32.c:1873: GetDC failed: Invalid window handle.

(enter_license.exe:210232): Gdk-WARNING **: gdkgc-win32.c:968: GetCurrentObject failed: The handle is invalid.

(enter_license.exe:210232): Gdk-WARNING **: gdkgc-win32.c:970: RestoreDC failed: The handle is invalid.

(enter_license.exe:210232): Gdk-CRITICAL **: _gdk_win32_drawable_release_dc: assertion `impl->hdc_count > 0' failed

(enter_license.exe:210232): Gdk-WARNING **: gdkwindow-win32.c:2216: SetWindowLongPtr failed: Invalid window handle.

当我尝试从单独的线程调用GTK对象时,听起来有些不对劲。 也许某种程度上我需要调用句柄(回调)来在主线程中实现“show_dialog”?

编译:

 gcc -IC:/MinGW/include -o enter_license enter_license.c  `pkg-config --libs --cflags gtk+-2.0 gthread-2.0``

流程:主要 - >叫“do_something” - >创建线程并调用“argument_thread” - >

以下是一段代码:

typedef struct _Data
{
 GtkWidget *win; 
 } Data;

主要

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

 gtk_init(&argc, &argv);
 window = do_something(NULL, argv);
 g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
 gtk_main();    

return 0;
}

do_something

GtkWidget * do_something(GtkWidget *do_widget, char **argv){
  ....
 GtkWidget *window;

if (!window){
window = gtk_dialog_new_with_buttons ("GtkSpinner",
                                      GTK_WINDOW (do_widget),
                                      0,
                                      GTK_STOCK_CLOSE,
                                      GTK_RESPONSE_NONE,
                                      NULL);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);

g_signal_connect (window, "response", G_CALLBACK (gtk_widget_destroy), NULL);
g_signal_connect (window, "destroy",  G_CALLBACK (gtk_widget_destroyed), &window);


 ....

if (!gtk_widget_get_visible (window)){
 gtk_widget_show_all (window);
}
else{
 gtk_widget_destroy (window);
}

    // define thread
    GThread*  thread;
    GError*   err;  
    Data data;

data.win = window;

     thread = g_thread_create((GThreadFunc)argument_thread,&data,FALSE, &err);
 return window;
}

show_dialog

gboolean show_dialog( GtkWidget* mw)
{
 GtkWidget *dialog;

  printf("BOO: \n");

  // here all works fine
  sleep(3000);
  gtk_widget_show(spinner_sensitive);
  gtk_spinner_start (GTK_SPINNER (spinner_sensitive));
  sleep(3000);
  gtk_spinner_stop (GTK_SPINNER (spinner_sensitive));
  sleep(3000);
  gtk_widget_hide(spinner_sensitive);

  printf("BOO\n");
  // here dialog is shown for 1-10 milisec and get error.

     dialog = gtk_message_dialog_new (GTK_WINDOW(mw),
                     GTK_DIALOG_DESTROY_WITH_PARENT,
                     GTK_MESSAGE_INFO,
                     GTK_BUTTONS_CLOSE,
                     "Downloaded successfully");

             g_signal_connect_swapped (G_OBJECT (dialog), "response",
                     G_CALLBACK (gtk_widget_destroy),
                     G_OBJECT (dialog));
             gtk_widget_show(dialog);
printf("BOO\n");

}

argument_thread

void *argument_thread( gpointer ptr ) {
  Data *data = (Data*)ptr;
gdk_threads_enter();
 show_dialog (data->win);
 gdk_threads_leave();
  return( NULL );
}

请帮助我,

非常感谢任何和所有建议

2 个答案:

答案 0 :(得分:3)

GTK不是线程安全的,所以任何与GUI交互的东西都必须在主线程上运行。

使用g_idle_add功能在下载完成后通知主线程。

答案 1 :(得分:0)

根据@VincentPovirk的说法,对于那些寻找答案的人来说,这是一个有效的实施方案:

进入线程我们调用g_idle_add并调用将在第二个Thread(也就是在main)之外实现的callback_func

gboolean show_dialog( GtkWidget* mw)
{
sleep(3000);
gtk_widget_show(spinner_sensitive);
gtk_spinner_start (GTK_SPINNER (spinner_sensitive));
sleep(3000);
gtk_spinner_stop (GTK_SPINNER (spinner_sensitive));
gtk_widget_hide(spinner_sensitive);

g_idle_add(callback_func, NULL);  
}

gint callback_func(void *unused)
{
  GtkWidget *dialog;
  dialog = gtk_message_dialog_new (GTK_WINDOW(window),
                     GTK_DIALOG_DESTROY_WITH_PARENT,
                     GTK_MESSAGE_INFO,
                     GTK_BUTTONS_CLOSE,
                     "Succeeded.");

             g_signal_connect_swapped (G_OBJECT (dialog), "response",
                     G_CALLBACK (gtk_widget_destroy),
                     G_OBJECT (dialog));
             gtk_widget_show(dialog);
return FALSE;

}