我如何在程序运行时更改标签的GTK;从单独的线程 [英] How do I change a label in GTK while the program is running; from a seperate thread
问题描述
我建立一个多线程的应用程序,它会显示天气数据和C.自动更新我得到的气象数据刷新并加载到变量。我无法在我的计划中端运行的变化值。当我使用
I am building a multi-threaded application which will display weather data and update automatically in C. I got the weather data to refresh and loaded into variables. I am having trouble changing values on my program mid-run. When I use
gtk_label_set_text(GTK_LABEL(wsrc->text2), wsrc->deg);
我得到
(out:7604): Gtk-CRITICAL **: gtk_label_set_text: assertion `GTK_IS_LABEL (label)' failed
我如何做到这一点。该呼叫是从主一个单独的线程循环更新的气象数据。
How do I do this. The call is from a separate thread from main that loops to renew weather data.
难道我用的信号来创建一个触发更新的气象资料。寻找建议:)
Do I use signals to create a trigger to renew weather data. Looking for advice :)
推荐答案
不要从外面的主线程,周期GTK +的方法。我知道有解决方法,但99.99%的时间,你真的不想这样做。
Do not call GTK+ methods from outside it's main thread, period. I know there are workarounds, but 99.99% of the time you really do not want to do it.
你应该做的,而不是是这样的:
What you should do instead is this:
/* in your other thread do this: it will make sure update_text2 will be called in
GTK+ main thread */
g_main_context_invoke (NULL, update_text2, wsrc);
static gboolean update_text2 (gpointer userdata)
{
my_obj* wsrc = (my_obj*) userdata;
gtk_label_set_text(GTK_LABEL(wsrc->text2), wsrc->deg);
return G_SOURCE_REMOVE;
}
我没有测试code,不知道你的WSRC指针的类型,但我敢肯定,你得到的漂移。
I didn't test that code, and don't know the type of your wsrc pointer but I'm sure you get the drift.
这篇关于我如何在程序运行时更改标签的GTK;从单独的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!