问题
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?
I tried using delegates but it doesn't seem to be working. Any ideas? Possibly examples? Thank you.
回答1:
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();
});
}
来源:https://stackoverflow.com/questions/2548200/how-do-i-access-gui-gtk-from-multi-threads