Re: Multi-threaded GUI



HI,

It's very easy to send messages back to the main loop, either during the
course of the thread's execution and/or to return control.

To have the main loop process an event generated by a separate thread,
simply use the g_idle_add() function, this call is always thread-safe. 
You can do this during the execution of the thread (to update progress) as
well as to return control to the main loop, or, rather, to let the main
loop know that the thread has completed its task.

Have a look at the following in the GTK+ FAQ (questions 5.2, 5.3, & 5.4):
http://www.gtk.org/faq/#AEN482

Of course, I am assuming the thread is not calling gtk+ routines itself. 
In which case, the thread must be initialized using the gtk+ thread
routines described in the FAQ above.  But to have a separate thread which
is gtk+-unaware, that also provides indications back to the gtk+ main loop
is completely straightforward, and it works.

good luck,

richard

> I am a new programmer to GTK and I am trying to wrap my head around
> several
> things. The biggest being the fact that GTK main is single threaded and
> blocking. I am using the gtkmm package.
>
> Basicaly what happens is that when a user presses a connect button on my
> interface, the callback fires up a seperate pthread to handle the
> connection. Unfortunately I want this new thread to update the interface
> regularly to let the user know it is trying to connect.
>
> I have tried using the following code:
>
> while(Gtk::Main::events_pending() )
> {
>        Gtk::Main::iteration();
> }
>
>
> Now GTK keeps complaining that it is being told to update from outside the
> Gtk::Main loop, and the program is EXTREMELY flaky, usualy dying on a
> signal
> 11 shortely.
>
> How do I get the main loop to update the interface without killing
> everything? I have tried finding information on the Glib::Dispatcher
> system
> but there is really no information on how it works, or how to use it.
>
> Any help would be greatly apreciated.
>
> Sincerely
> Melvin Newman
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
>





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