开发者

How do I access GUI (GTK) from multi threads?

I have a worker thread spawned from a GUI (for GUI performance), how do I access GUI, such as spawning new windows/widgets from the thread itself?开发者_Python百科

I tried using delegates but it doesn't seem to be working. Any ideas? Possibly examples? Thank you.


According to their Best Practices:

Gtk# is not a thread-safe toolkit, which means that only one thread at a time can safely invoke methods on Gtk#. This thread is typically the thread executing the main loop (which is when control has been explicitly transfered to Gtk).

When application developers need to have threads update some element of the graphical user interface they have to either acquire a lock that allows them to issue Gtk# toolkit invocations or they can make their code execute on the same thread as the one thread that executes the main loop.

To invoke a method on the GTK+ main loop thread and avoid any threading problems with GTK, you can use the Gtk.Application.Invoke() method (if you are targetting Gtk# 1.0 you can use Gtk.ThreadNotify).

The following example is provided; you should use Invoke to execute any Gtk code from within the main loop:

public void ThreadedMethod()
{
    Gtk.Application.Invoke(delegate {
        do_stuff_in_main_thread();
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜