Re: GTK app misbehaving. (main thread blocking)



On Saturday 24 April 2004 17:25, Alexander Markley wrote:

I've got a button and a status-bar. I connect a callback to the button's
'clicked' event. The callback does A) a lot of work unrelated to GTK,
and B) updates the status-bar with the current progress.

In my mind, this should work. Except, while the callback is doing its
work, the main GTK window is completely frozen. The status bar doesn't
get updated, and the application window doesn't redraw. (If I cover the
window then reveal it, it stays blank.)

So my question is this: What is the correct way of doing bunches of work
when a signal is emitted without locking up the main thread?

You have two options:

(a)  run   

      while (gtk_events_pending()) 
         gtk_main_iteration_do(FALSE);

in your inner 'work loop' and after you update the progressbar/statusbar. (if 
you are doing blocking I/O, you might want to look into GIOChannels instead). 


(b)  hook up an idle timeout with  g_idle_add (work_func, work_data) and do 
only a little bit of work every time the work_func() is called. Return TRUE 
if your function should be called again, and FALSE if you're done with your 
calculations. Obviously, this approach is only suitable for certain types of 
'background tasks'.

See also the Gtk+ FAQ. (http://www.gtk.org/faq/ is down currently, but you 
should be able to find a local copy somewhere in /usr/share/doc/ 
or /usr/local/share/doc if you have the gtk docs installed).

Cheers
-Tim



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