当前位置:网站首页>[QT dot] realize the watchdog function to detect whether the external program is running
[QT dot] realize the watchdog function to detect whether the external program is running
2022-06-27 06:09:00 【Lin Qi sevenlin】
Watchdog function
- Check whether the external program is running , If not running , Then start the external program
- Implement in the child thread , The main interface is not stuck
- Multiple external programs can be detected
.h file
#ifndef WATCHDOGTHREAD_H
#define WATCHDOGTHREAD_H
#include <QObject>
#include <QThread>
#include <QMap>
class WatchDogThread : public QThread
{
Q_OBJECT
public:
WatchDogThread();
public:
// Add external program absolute path ( Path cannot contain spaces )
void appendProcess(const QString &processPath);
void appendProcess(const QList<QString> &processPathList);
// Set detection frequency
void setInterval(int msec);
protected:
void run() override;
private:
// Check whether the program is running
bool checkProcess(const QString &processName);
// Start an external program
bool startProcess(const QString &processPath);
signals:
void signal_sendMsg(const QString &msg);
private:
QMap<QString, QString> m_processMap; // key: The program name value: Program absolute path
int m_interval; // Detection frequency
};
#endif // WATCHDOGTHREAD_H
.cpp file
#include "watchdogthread.h"
#include <QTimer>
#include <QProcess>
WatchDogThread::WatchDogThread()
{
m_interval = 1000 * 3;
}
void WatchDogThread::appendProcess(const QString &processPath)
{
QString processName = processPath.split("/").last();
m_processMap.insert(processName, processPath);
}
void WatchDogThread::appendProcess(const QList<QString> &processPathList)
{
for (const auto &path : processPathList) {
QString processName = path.split("/").last();
m_processMap.insert(processName, path);
}
}
void WatchDogThread::setInterval(int msec)
{
m_interval = msec;
}
void WatchDogThread::run()
{
QTimer timer;
timer.setInterval(m_interval);
connect(&timer, &QTimer::timeout, this, [&] {
QList<QString> processNameList = m_processMap.keys();
for (const auto &name : processNameList) {
if (checkProcess(name)) {
} else {
if (startProcess(m_processMap.value(name))) {
emit signal_sendMsg(QString(" Program [%1] Restart successful ").arg(name));
} else {
emit signal_sendMsg(QString(" Program [%1] Restart failed ").arg(name));
}
}
}
});
timer.start();
exec();
}
bool WatchDogThread::checkProcess(const QString &processName)
{
QProcess process;
#ifdef Q_OS_WIN
/***
tasklist View system task list
/fi (filter) Displays a list of processes that match the filter
eq equal
imagename Image name
eg: tasklist /fi "imagename eq QtCreator.exe"
Image name PID Conversation name conversation # Memory usage
========================= ======== ================ =========== ============
qtcreator.exe 22188 Console 1 133,732 K
qtcreator.exe 21008 Console 1 189,648 K
***/
QString cmd = QString("tasklist /fi \"imagename eq %1\"").arg(processName);
process.start(cmd);
#else
QStringList arguments;
arguments << QString("-c"); // I need to add one -c Parameter command can be executed
arguments << QString("ps -A | grep %1").arg(processName);
process.start("bash", arguments);
#endif
process.waitForFinished();
QString result = QString::fromLocal8Bit(process.readAllStandardOutput());
if (result.contains(processName, Qt::CaseInsensitive)) {
return true;
}
return false;
}
bool WatchDogThread::startProcess(const QString &processPath)
{
// Path with space , Use parametric mode , You can start
// process->start("C:/Program Files/Exe/111.exe", QStringList("C:/Program Files/Exe/111.exe"));
return QProcess::startDetached(processPath);
}
边栏推荐
- Dev++ 环境设置C语言关键字显示颜色
- js实现双向数据绑定
- expect脚本中使用scp命令的方法,expect脚本中scp命令获取不了值的问题完美解决方法
- How to check the frequency of memory and the number of memory slots in CPU-Z?
- [collection] Introduction to basic knowledge of point cloud and functions of point cloud catalyst software
- Go log -uber open source library zap use
- Altium Designer 19 器件丝印标号位置批量统一摆放
- 【养成系】常用正则表达式
- 30个单片机常见问题及解决办法!
- 汇编语言-王爽 第9章 转移指令的原理-笔记
猜你喜欢

How win 10 opens the environment variables window

Kubesphere cluster configuration NFS storage solution - favorite

openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法

JVM对象组成和存储

Gaussian distribution, linear regression, logistic regression

Win 10 如何打开环境变量窗口

Us camera cloud service scheme: designed for lightweight video production scenes

卷积神经网络---CNN模型的应用(找矿预测)

汇编语言-王爽 第11章 标志寄存器-笔记
[email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT"/>NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
随机推荐
多线程带来的的风险——线程安全
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
我对于测试团队建设的意见
Contents in qlistwidget are not displayed
1317. convert an integer to the sum of two zero free integers
426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
openresty使用文档
TiDB 中的SQL 基本操作
Win 10 如何打开环境变量窗口
Proxy-Reflect使用详解
cpu-z中如何查看内存的频率和内存插槽的个数?
思维的技术:如何破解工作生活中的两难冲突?
KubeSphere 集群配置 NFS 存储解决方案-收藏版
WebRTC系列-网络传输之7-ICE补充之提名(nomination)与ICE_Model
TiDB 数据库快速上手指南
TiDB的使用限制
高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression
JVM garbage collection mechanism
创建一个基础WDM驱动,并使用MFC调用驱动
免费的 SSH 和 Telnet 客户端PuTTY