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

标签 c# mono gtk gtk#

我有一个从 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/

相关文章:

javascript - 按钮加载状态 Asp.net JavaScript

linux - mkbundle System.ComponentModel.Win32Exception 错误

debugging - 单声道调试信息在 debian 上有异常(exception)?

c# - 是什么导致 Mono 下的 SerialPort.Write 出现偶发错误?

c gtk+-2 林间空地 3 : adding text to a combobox loaded from Gtk Builder

c++ - Gtk:将焦点发送到顶层窗口而不丢失第一个顶层窗口

opencv - 运行时 Opencv HighGui 错误 - "HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP"。 opencv交叉编译。主机=64 位 Ubuntu 12.04。目标=ARM-Cortex-A9

C# ToDictionary 在 C# 7 之前获取匿名类型值

c# - 将 System.Data.SqlClient 升级到版本 4.5.0-preview2 后,Dapper 引发 System.Data.SqlClient.SqlConnection 异常

c# - 无法加载文件或程序集 System.ServiceModel,或其依赖项之一。没有足够的存储空间来处理这个命令