Re: GTK window and thread



On 5/2/05, Frederic Beck <frederic beck loria fr> wrote:
> Hello
> 
> Here's my problem : i have a daemon running in the background, and i
> want it to show popups after some events.
> 
> I'm new in using GTK, so i took a look at the tutorial, and i managed to
> create the window and show it. The popup window is closed when pressing
> on a button.
> 
> As i don't want this popup to be blocking for the daemon, i wanted to
> put it in a thread using pthread.
> 
> After a few tries, i managed to make it work, which means, i can show
> multiple windows at the same time and close them independently.
> 
> To do so, in the function which shows the popup window, i have the
> initialisation of the window, and then the following liness :
> 
> gtk_main ();
> while (gtk_events_pending())
>         gtk_main_iteration();
> 
> and a quit handler is associated to the button and contains :
> 
> gtk_main_quit ();
> 
> I have 2 questions :
> 
> 1. When running it works, but i have lots of messages like :
> 
> (<unknown>:20080): GLib-WARNING **: g_main_context_check() called
> recursively from within a source's check() or prepare() member.
> 
> (<unknown>:20080): GLib-WARNING **: g_main_context_prepare() called
> recursively from within a source's check() or prepare() member.
> 
> 2. (more about pthread i guess) i don't want to add a join in my
> program, i just want the thread to show the window and stop when the
> button is clicked. That's why, after everything is done, i added a
> ptrhead_exit(NULL) call, but even after the window has been closed, i
> still have processes  :
> 
> 20287 pts/1    Z      0:00  \_ [sh] <defunct>
> 20292 pts/1    Z      0:00  \_ [sh] <defunct>
> 20296 pts/1    Z      0:00  \_ [sh] <defunct>
> 20302 pts/1    Z      0:00  \_ [sh] <defunct>
> 
> i have 4 such processes for each window opened. Am i doing something
> wrong with GTK or pthread ?
> 

You can create a thread (using GLib threads handling functions) in
which _only_ the deamon works, and one thread for _all_ GTK related
stuff. You need to call g_thread_init() but you don't need to call
gtk_threads_init() since the GTK main loop remains in a single thread
and from GTK, you only have one thread. This way you have a real
separation of the daemon part and the GUI part.

You must make sure though that no GTK+ call is made from within any
GLib callback in the thread running the daemon: I use a GAsyncQueue to
make the two threads communicate: the daemon thread "writes" to the
queue when it has an event, and the GTK thread has an idle function
(see g_idle_add) which reads from that queue and updates the GUI.

Vivien



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