[Gtk-sharp-list] Problem with threaded GTK# Application on Windows

Chris Howie cdhowie at gmail.com
Mon Feb 23 08:51:06 EST 2009


On Mon, Feb 23, 2009 at 8:44 AM, Rapha222 <raphaeljavaux at gmail.com> wrote:
> GTK is thread safe, so you can't to modify Gtk Widgets from another thread
> that the thread which is running the MainLoop (Application.Run()), unless if
> you use Thread.Enter() and Thread.Leave() to stop the mainloop during the
> modifications.

I assume here you mean that it is not thread-safe.

Regardless, you can simulate this by using the provided Gtk.Application.Invoke:

void ThreadEntryPoint() {
    // do stuff...
    Application.Invoke(delegate {
        // do stuff with widgets
    });
    // do more stuff...
}

The call is not synchronous, and will return immediately.  The
delegate will be executed at some point later on the main thread.  If
you need a synchronous version:

void SynchronousInvoke(EventHandler e) {
    ManualResetEvent ev = new ManualResetEvent(false);

    Application.Invoke(delegate(object o, EventArgs args) {
        e(o, args);
        ev.Set();
    }

    ev.WaitOne();
}

It's not a terribly efficient method but if you're not making these
calls that often the performance should be fine.

-- 
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers


More information about the Gtk-sharp-list mailing list