Hi geoffjay!! thank you for reply!!
You are right, I have to use those calls. This code works properly
Code:
#include <gtk/gtk.h>
#include <stdlib.h>
#include <pthread.h>
GtkWidget *window;
gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data )
{
gtk_main_quit ();
return FALSE;
}
void *thread_dialog()
{
gdk_threads_enter ();
GtkWidget *label;
GtkWidget *dialog=gtk_dialog_new();
label=gtk_label_new("just a label");
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),label, TRUE, TRUE, 0);
gtk_widget_show(label);
gtk_dialog_add_button(GTK_DIALOG(dialog),"Accept",GTK_RESPONSE_ACCEPT);
gtk_dialog_add_button(GTK_DIALOG(dialog),"Reject",GTK_RESPONSE_REJECT);
gint ret= gtk_dialog_run (GTK_DIALOG (dialog));
/* take action depending on ret */
printf("value=%d\n",ret);
gtk_widget_destroy(dialog);
gdk_threads_leave ();
return NULL;
}
void init_dialog()
{
pthread_t dialog_req;
if(pthread_create(&dialog_req,NULL,thread_dialog,NULL)!=0)
perror("error creating tread\n");
}
int main( int argc, char *argv[] )
{
g_thread_init (NULL);
gdk_threads_init ();
gtk_init (&argc, &argv);
GtkWidget *button;
GtkWidget *box1;
gtk_rc_parse("rc_file");
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window), "delete_event",G_CALLBACK (delete_event), NULL);
box1 = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box1);
button = gtk_button_new_with_label ("Button 1");
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (init_dialog), (gpointer) "button 1");
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
gtk_widget_set_name (button, "special button");
gtk_widget_show (button);
gtk_widget_show (box1);
gtk_widget_show (window);
gdk_threads_enter ();
gtk_main ();
gdk_threads_leave ();
return 0;
}
This is a summary of a much larger code. I only wanted to post the problem, not the entire code.
I am developing a network application. So a server must be listening using “accept” call, which blocks, so that's why I need another thread. The main thread for the main application, other thread listening on clients to “connect”. When a connection from a client arrives, a dialog is shown, using gtk_dialog_run (GTK_DIALOG (dialog));
So my problem now is that if I use a server thread, it is always running, will never call to gdk_threads_leave. This is a pseudo code example
Code:
gdk_threads_enter();
while(1)
{
accept_connections_from_client
}
gdk_threads_leave(); will never be executed
So right now the only solution is to wrap gtk_dialog_run (GTK_DIALOG (dialog)); with these calls.
Code:
gdk_threads_enter();
gtk_dialog_run (GTK_DIALOG (dialog));
gdk_threads_leave();
Do you find other solution??
Thank you very very much!!!!