问题描述
我一直在尝试在我的 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,我尝试在显示所有子功能后启动线程,但发生同样的情况,感谢任何帮助:)>
#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 (将#修改为@)