[Gtk-sharp-list] Responsive Applications

Felipe Almeida Lessa felipe.lessa at gmail.com
Sat Jan 6 13:31:28 EST 2007

On 1/6/07, White Spirit <wspirit at homechoice.co.uk> wrote:
> Matteo Bertozzi wrote:
>  > Why OnButtonClick() Code Freeze the GUI?
>  > and how i could correct it?
> The problem is here:
>  >         Gtk.Application.Invoke (delegate {
>  >             Thread.Sleep(1800 + 3600);
>  >             button.Label = "End Hard Work";
>  >         });

Yes, the problem is there.

> The code in the event handler is already running in the GTK thread.
> Your code attempts to pass the code to the GTK thread from within the
> GTK thread.
> Gtk.Application.Invoke is only used within threads other than the GTK
> thread (because GTK# is not 'threadsafe').  So, if you had a thread
> called mythread, once that thread was started you would use
> Gtk.Application.Invoke to access GTK objects safely.

In fact, you can use Gtk.Application.Invoke everywhere. As you said,
he's already on the GTK's thread, so Gtk.Application.Invoke just
executes the code right away.

The problem, in fact, is that he is using Thread.Sleep in the GTK's
thread, so this blocks the UI. If this sleep is simulating some work
being done, it should be put on a different thread like this:

Thread worker = new Thread(delegate {
    Thread.Sleep(1800 + 3600);
    // We're on a different thread, Gtk.Application.Invoke is mandatory
    Gtk.Application.Invoke(delegate {
        button.Label = "End Hard Work";


More information about the Gtk-sharp-list mailing list