[Gtk-sharp-list] GTK# app "hanging"

Jonathan Pryor jonpryor@vt.edu
Tue, 22 Feb 2005 06:54:42 -0500


On Tue, 2005-02-22 at 10:46 +0000, Manuel Capinha wrote:
> The watcher object is threaded and is responsible for updating the
> images. This is done by creating a WebRequest object that points to
> the image in a webserver. The Gdk.Image object is updated with:
> 
>     image.Pixbuf = new Gdk.Pixbuf(request.GetResponse().GetResponseStream());

Assuming that the thread that executes the above is NOT your GUI thread,
that is your problem. :-)

In GTK+/Gtk#, you can only update the GUI from the GUI thread.  All
other threads MUST NOT modify the GUI, as this will cause random hangs
(like you're seeing).

The solution is to add a callback to the GUI thread which will update
the GUI.  Callbacks can be added via GLiub.Timeout.Add.

In C# 2.0, you could do:

	Stream stream = request.GetResponse().GetResponseStream();
	GLib.Timeout.Add (0, delegate {
		image.Pixbuf = new Gdk.Pixbuf (stream);
	});

C# 1.1 implementations are left as a problem for the reader. :-)

 - Jon