微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过多线程 C++ GTKMM 运行 UI 每 5 秒获取一次传感器信息

如何解决通过多线程 C++ GTKMM 运行 UI 每 5 秒获取一次传感器信息

我一直在尝试在我的 UI(gtkmm3) 中显示传感器信息,使用 C++ 库“线程”,在调用函数 show_all_children(); 之前我会运行:

std::thread t1(get_sensor_info_thread);
t1.join();

调用这个函数(在构造函数外定义):

void get_sensor_info_thread() {
    while (true) {
        system("sensors");
        sleep(5);
    }
}

我的程序执行但仅每 5 秒在终端中显示传感器信息并且不显示 UI,我尝试在显示所有子功能后启动线程,但发生同样的情况,感谢任何帮助:)>

我的构造函数和 get_sensor_info 代码

#include "headers/MainWindow.h"
#include <ctime>
#include <iostream>
#include <string>
#include <thread>

void get_sensor_info_thread() {
    while (true) {
        system("sensors");
        sleep(5);
    }
}

MainWindow::MainWindow() :
mBox(Gtk::ORIENTATION_VERTICAL),headerBox(Gtk::ORIENTATION_HORIZONTAL),nBox1(Gtk::ORIENTATION_HORIZONTAL),lBox(Gtk::ORIENTATION_VERTICAL),rBox(Gtk::ORIENTATION_VERTICAL),sysBox(Gtk::ORIENTATION_VERTICAL),fanBox(Gtk::ORIENTATION_VERTICAL),cpuBox(Gtk::ORIENTATION_HORIZONTAL),gpuBox(Gtk::ORIENTATION_HORIZONTAL),moboBox(Gtk::ORIENTATION_HORIZONTAL) {

    set_title("NZXT Control UI");
    set_size_request(1000,600);
    set_resizable(false);
    add(mBox);

    mBox.pack_start(headerBox,Gtk::PACK_SHRINK);
    mBox.pack_start(mNotebook);

    prevButton.set_image_from_icon_name("go-prevIoUs",Gtk::ICON_SIZE_BUTTON);
    nextButton.set_image_from_icon_name("go-next",Gtk::ICON_SIZE_BUTTON);

    headerBox.pack_end(nextButton,Gtk::PACK_SHRINK);
    headerBox.pack_end(prevButton,Gtk::PACK_SHRINK);

    nextButton.signal_clicked().connect(sigc::mem_fun(*this,&MainWindow::on_next_page));
    prevButton.signal_clicked().connect(sigc::mem_fun(*this,&MainWindow::on_prev_page));

    mNotebook.popup_enable();
    mNotebook.set_scrollable();
    mNotebook.append_page(nBox1,"Stats",true);
    mNotebook.append_page(nBox2,"Fans",true);
    mNotebook.append_page(nBox3,"RGB",true);

    nBox1.pack_start(lBox);
    nBox1.pack_start(rBox);

    lTitle.set_text("System statistics");
    rTitle.set_text("Fan/AIO speed statistics");
    
    lBox.pack_stbar button or the CTRL+K keyboard shortcut. For more editing help,click the [?] toolbar icon.art(lTitle,false,0);
    rBox.pack_start(rTitle,0);

    sysFrame.set_label("System");
    fanFrame.set_label("Fans");
    lBox.add(sysFrame);
    rBox.add(fanFrame);

    cpuTemp = 0.0;
    gpuTemp = 0.0;
    moboTemp = 0.0;

    cpuLabel.set_text("cpu temperature: ");
    gpuLabel.set_text("GPU temperature: ");
    moboLabel.set_text("motherboard temperature: ");
    cpuEntry.set_text(std::to_string(cpuTemp));
    gpuEntry.set_text(std::to_string(gpuTemp));
    moboEntry.set_text(std::to_string(moboTemp));

    cpuBox.pack_start(cpuLabel,0);
    cpuBox.pack_end(cpuEntry,0);
    gpuBox.pack_start(gpuLabel,0);
    gpuBox.pack_end(gpuEntry,0);
    moboBox.pack_start(moboLabel,0);
    moboBox.pack_end(moboEntry,0);

    sysBox.pack_start(cpuBox,0);
    sysBox.pack_start(gpuBox,0);
    sysBox.pack_start(moboBox,0);

    sysFrame.add(sysBox);
    mBox.set_name("main-Box");
    nBox1.set_name("stats-Box");
    lBox.set_name("left-Box");
    rBox.set_name("right-Box");

    auto css = Gtk::Cssprovider::create();
    css->load_from_path("style.css");
    Gtk::StyleContext::add_provider_for_screen
                   (Gdk::Screen::get_default(),css,GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

    show_all_children();

    std::thread t1(get_sensor_info_thread);
    t1.join();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。