GTK3和多线程,取代德precated功能 [英] GTK3 and multithreading, replacing deprecated functions

查看:690
本文介绍了GTK3和多线程,取代德precated功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想换成德precated功能 gdk_threads_enter()/离开()在我的应用程序,使用线程。像现在的应用,可以完美运行(虽然我不知道这是否是做的正确方法的)。

I would like to replace deprecated functions gdk_threads_enter()/leave() in my application that uses threads. The application as it is now, works perfect (although i am not sure if this is the right way to do it).

我的主循环,运行进入主循环和信号处理程序。当我收到一个启动按钮,我启动一个线程,在沿主后台运行。如何更新从该线程的GUI。我每GTK3和GDK3的文档都知道,他们说,通过避免它

My main loop, runs the gtk_main and the signal handlers. When i receive a start button, i start a thread, that runs in the background along the main. How can i update the GUI from that thread. I know per the Documentation of GTK3 and GDK3, they say avoid it by using

gdk_threads_add_idle() 

gdk_threads_add_timeout() 

但我怎么做,如果我想只完成更新,当我点击开始?
有没有例子。我不问如何使用 gdk_threads_add_idle(),我问如何在主点击开始后,运行工人功能,无需线程。

But how do I do this if I want the updating to be done only when I click start? is there any example. I am not asking how to use gdk_threads_add_idle(), I am asking how to run worker function in the main without a thread after clicking start.

按钮点击 - >pviously螺纹$ P $启动工作功能 - >更新大量的GUI窗口GUI元素

推荐答案

什么文件说的是,你仍然可以运行你的工人功能的一个线程,你就不能使用GTK和GDK功能从那个线程。所以,你仍然可以启动线程当您单击开始。但是,而不是从线程更新界面元素,您需要通过安排他们从主线程更新 gdk_threads_add_idle()

What the documentation says is that you can still run your worker function in a thread, you just can't use GTK and GDK functions from that thread. So, you can still start the thread when you click start. But instead of updating GUI elements from the thread, you have to schedule them to be updated from the main thread by using gdk_threads_add_idle().

所以,你的图应该是这个样子:

So your diagram should look something like this:

Main thread     Worker thread
    |
Button clicked
    |      \________
    |               \
    |           Start worker function
    |                |
    |           Computation
    |                |
    |           Want to update GUI
    |                |
    |           gdk_threads_add_idle(function1, data1)
    | ______________/|
    |/               |
    v           More computation
function1 runs       |
    |           Want to update GUI
GUI updated          |
    |           gdk_threads_add_idle(function2, data2)
    | ______________/|
    |/               |
    v           More computation
function2 runs       |
    |      
  etc...

如果这是你的使用情况太复杂,你有你的工作线程运算的控制权返回给你的工作线程往往不够(比如,你正在计算在一个循环的东西),那么你完全可以运行的计算在没有通过短暂返回控制到GUI主循环,像这样锁定了GUI主线程:

If this is too complicated for your use case, and you have a computation in your worker thread that returns control to your worker thread often enough (say, you are calculating something in a loop), then you can run the calculation entirely in the main thread without locking up the GUI by briefly returning control to the GUI main loop, like so:

for (lots of items) {
    result = do_short_calculation_on(one_item);

    update_gui(result);

    while (gtk_events_pending())
        gtk_main_iteration();
}

这篇关于GTK3和多线程,取代德precated功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆