gpt4 book ai didi

c# - 如何从多线程访问 GUI (GTK)?

转载 作者:可可西里 更新时间:2023-11-01 03:06:08 26 4
gpt4 key购买 nike

我有一个从 GUI 生成的工作线程(用于 GUI 性能),我如何访问 GUI,例如从线程本身生成新窗口/小部件?

我尝试使用委托(delegate),但它似乎不起作用。有任何想法吗?可能的例子?谢谢。

最佳答案

根据他们的 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).

提供了以下示例;你应该使用 Invoke 从主循环中执行任何 Gtk 代码:

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

关于c# - 如何从多线程访问 GUI (GTK)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2548200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com