c++ - 通过多线程 C++ GTKMM 运行 UI,每 5 秒获取一次传感器信息
问题描述
我一直在尝试使用 c++ 库“线程”在我的 UI(gtkmm3)中显示传感器信息,在调用该函数之前,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, false, 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, false, 0);
cpuBox.pack_end(cpuEntry, false, 0);
gpuBox.pack_start(gpuLabel, false, 0);
gpuBox.pack_end(gpuEntry, false, 0);
moboBox.pack_start(moboLabel, false, 0);
moboBox.pack_end(moboEntry, false, 0);
sysBox.pack_start(cpuBox, false, 0);
sysBox.pack_start(gpuBox, false, 0);
sysBox.pack_start(moboBox, false, 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();
}
解决方案
推荐阅读
- c# - xml.Load - 根级别的数据无效。第 1 行,位置 1
- javascript - 当我期望一个数组时,Fetch 返回承诺
- php - 我想在遍历获取的行时创建二维数组
- node.js - 异步函数中未定义的返回数据
- reactjs - 使用 map 函数填充表时返回 2 行
- php - 字符串函数:preg-replace
- python - 我们可以从没有实例化的类中调用方法吗
- intel - Perf 事件的含义是什么:dTLB-loads 和 dTLB-stores?
- sql - 在 TO_CHAR 的 Select 语句中设置默认列值
- java - 如何阅读 Elasticsearch 快照