- 论坛徽章:
- 0
|
原帖由 惠繪洋 于 2006-12-4 11:05 发表
謝謝savageranthony , 再向你請教.
現在我的程序已經使用了 gdk_thread.
- g_thread_init(NULL);
- gdk_threads_init();
- gdk_threads_enter();
- ... ... ...
- gtk_main();
- gdk_threads_leave();
- ...
复制代码
在 gtk_signal_connect 指定的信号调用中不能使用 gdk_threads_enter/gdk_threads_leave
因为 gdk_threads_enter/gdk_threads_leave 不能嵌套,所以你的程序会当掉。
用法示例(随手写写,可能有错):
- /* 主线程 */
- g_thread_init(NULL);
- gdk_threads_init(NULL);
- gdk_threads_enter();
- ...
- gtk_signal_connect(GTK_OBJECT(aObject), "something", GTK_SIGNAL_FUNC(aFunc), user_data);
- ...
- gtk_main();
- gdk_threads_leave();
- /* 信号调用 */
- static void aFunc(GtkWidget *widget, GdkEvent *event, gpointer user_data)
- {
- gpointer flag = g_object_get_data(G_OBJECT(widget), "something_for_thread");
- if(flag == NULL)
- {
- ...
- g_thread_create(G_THREAD_FUNC(threadFunc), widget, FALSE, NULL);
- ...
- }
- else
- {
- ....
- g_object_set_data(G_OBJECT(widget), "something_for_thread", NULL);
- }
- }
- /* 线程调用 */
- static void threadFunc(GtkWidget *widget)
- {
- gdk_threads_enter();
- if(GTK_WIDGET_MAPPED(widget) == FALSE) gtk_widget_show(widget);
- if(GTK_WIDGET_REALIZED(widget) == FALSE) gtk_widget_realize(widget);
- ... /* do something */
- /* tell the main loop that I'm finished */
- g_objcet_set_data(G_OBJECT(widget), "something_for_thread", widget);
- gtk_singal_emit_by_name(G_OBJECT(widget), "something", NULL);
- gdk_threads_leave();
- }
复制代码
[ 本帖最后由 savageranthony 于 2006-12-4 14:01 编辑 ] |
|