1

我有一个非常简单的应用程序,带有文本输入和按钮。

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

在此处输入图像描述

由于我不知道连接下载文件需要多长时间,我为此使用了单独的线程。但是在“对话框显示”上,我的应用程序失败了,并且出现了以下错误:

(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``

流程:main -> 调用“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;
}

做点什么

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;
}

显示对话框

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");

}

参数线程

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

请帮我,

任何和所有的建议将不胜感激

4

2 回答 2

3

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

下载完成时使用该g_idle_add函数通知您的主线程。

于 2012-12-06T23:32:13.787 回答
0

根据@VincentPovirk 的说法,仅针对寻求答案的人,这是一个有效的实现:

进入我们调用g_idle_add和调用的线程,该线程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;

}
于 2012-12-06T23:59:12.713 回答