If I have to call the gtk_dialog_run() in new thread, How shall I do?



Hello,
When I run gtk_dialog_run() in new created thread, an error occurs.
Nevertheless, I can not find out the reason. I only want to immitate the
action: open a "Find" dialog, then click "Find Next" button, then launch
a thread that will do some search, then popup a dialog in the thread
which will prompt that the search have been done.
Following is my code, thanks advance!
------------


//gcc -o tdlg tdlg.c `pkg-config --cflags --libs gtk+-2.0` -lgthread-2.0 -g
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>

#define _(x) x
GtkWidget *win = NULL;

static gpointer ThreadFun(gpointer arg)
{
GtkWidget *pWin = GTK_WIDGET(arg);
GtkWidget *pDlg = gtk_message_dialog_new (GTK_WINDOW(pWin),
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s",
"HELLO YHT");
gtk_dialog_run( GTK_DIALOG(pDlg));
gtk_widget_destroy(pDlg);
}


void RunThread(GtkButton *pBtn, gpointer user_data)
{
g_thread_create(ThreadFun, win, TRUE, NULL);
/*if I call the callback function directly, then Everything will be OK.
However, if I
launch a thread in which It Call the gtk_dialog_run(), as a result, a
Error occur.Could
you help me find out the reason? If I have to call the gtk_dialog_run()
in new thread, How
shall I do?*/

//ThreadFun(win);

}



GtkWidget*
create_dlg_popup (void)
{
GtkWidget *dlg_popup;
GtkWidget *dialog_vbox1;
GtkWidget *btn_runthread;
GtkWidget *dialog_action_area1;
GtkWidget *okbutton1;

dlg_popup = gtk_dialog_new ();
gtk_window_set_title (GTK_WINDOW (dlg_popup), _("Popup"));
gtk_window_set_modal (GTK_WINDOW (dlg_popup), TRUE);
gtk_window_set_type_hint (GTK_WINDOW (dlg_popup),
GDK_WINDOW_TYPE_HINT_DIALOG);

dialog_vbox1 = GTK_DIALOG (dlg_popup)->vbox;
gtk_widget_show (dialog_vbox1);

btn_runthread = gtk_button_new_with_mnemonic (_("RunThread"));
gtk_widget_show (btn_runthread);
gtk_box_pack_start (GTK_BOX (dialog_vbox1), btn_runthread, FALSE, FALSE, 0);
g_signal_connect (btn_runthread, "clicked", G_CALLBACK(RunThread), NULL);

dialog_action_area1 = GTK_DIALOG (dlg_popup)->action_area;
gtk_widget_show (dialog_action_area1);
gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1),
GTK_BUTTONBOX_END);

okbutton1 = gtk_button_new_from_stock ("gtk-ok");
gtk_widget_show (okbutton1);
gtk_dialog_add_action_widget (GTK_DIALOG (dlg_popup), okbutton1,
GTK_RESPONSE_OK);
GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
return dlg_popup;
}

void FunPopup(GtkButton *pBtn, gpointer user_data)
{
GtkWidget *pDlg = create_dlg_popup();
gint ret = gtk_dialog_run(GTK_DIALOG(pDlg));
if(ret == GTK_RESPONSE_OK)
{
return;
}
}


GtkWidget*
create_win (void)
{

GtkWidget *table1;
GtkWidget *btn_popup;
GtkWidget *btn_cancel;

win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show(win);
gtk_window_set_title (GTK_WINDOW (win), _("window1"));

table1 = gtk_table_new (1, 2, TRUE);
gtk_widget_show (table1);
gtk_container_add (GTK_CONTAINER (win), table1);

btn_popup = gtk_button_new_with_mnemonic (_("Popup"));
gtk_widget_show (btn_popup);
gtk_table_attach (GTK_TABLE (table1), btn_popup, 0, 1, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
g_signal_connect(btn_popup, "clicked", G_CALLBACK(FunPopup), NULL);

btn_cancel = gtk_button_new_with_mnemonic (_("Cancel"));
gtk_widget_show (btn_cancel);
gtk_table_attach (GTK_TABLE (table1), btn_cancel, 1, 2, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);

return win;
}


int main(int argc, char* argv[])
{
gtk_init(&argc, &argv);
if (!g_thread_supported ())
{
g_thread_init(NULL);
gdk_threads_init();
}
create_win();
gtk_main();
return 0;
}

-- 
Sincerely,
Alfred Young
R&D, Application Architectures
www.foxitsoftware.com



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