如何从多线程访问GUI(GTK)? [英] How do I access GUI (GTK) from multi threads?
问题描述
我有一个工作线程从GUI(用于GUI性能),我怎么访问GUI,如线程本身产生新的窗口/部件催生了?
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.
推荐答案
根据他们的最佳实践的:
Gtk#的是不是线程安全的工具包,这意味着,只有一次一个线程可以安全地调用Gtk#的方法。这个线程通常执行主循环(这是在控制已明确转移到基于GTK)的主题。
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).
当应用程序开发人员需要有线程更新的一些元素的图形用户界面,他们必须要么获得锁,允许他们发出的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.
要调用GTK +主循环线程的方法,避免与任何GTK线程问题,您可以使用Gtk.Application.Invoke()方法(如果你是打靶。Gtk#的1.0可以使用Gtk.ThreadNotify)
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).
提供下面的例子;你应该使用调用
从主循环中执行任何的Gtk代码:
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();
});
}
这篇关于如何从多线程访问GUI(GTK)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!