当前位置:网站首页>[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);
}
边栏推荐
- LeetCode 0086. Separate linked list
- Contents in qlistwidget are not displayed
- IAR systems fully supports Centrino technology 9 series chips
- [getting started] regular expression Basics
- How to check the frequency of memory and the number of memory slots in CPU-Z?
- 多线程带来的的风险——线程安全
- 卷积神经网络---CNN模型的应用(找矿预测)
- LeetCode 0086.分隔链表
- 310. minimum height tree
- Keep 2 decimal places after multiplying SQLSEVER fields
猜你喜欢
[email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT"/>NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT

高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression

使用 WordPress快速个人建站指南

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

函数栈帧的形成与释放

IAR Systems全面支持芯驰科技9系列芯片

创建一个基础WDM驱动,并使用MFC调用驱动

IAR systems fully supports Centrino technology 9 series chips

线程间等待与唤醒机制、单例模式、阻塞队列、定时器

My opinion on test team construction
随机推荐
693. alternate bit binary number
树莓派4B上运行opcua协议DEMO接入kubeedge
资深【软件测试工程师】学习线路和必备知识点
Altium designer 19 device silk screen label position shall be placed uniformly in batches
C Primer Plus 第11章_字符串和字符串函数_代码和练习题
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
426-二叉树(513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树、654. 最大二叉树)
WebRTC系列-網絡傳輸之7-ICE補充之提名(nomination)與ICE_Model
310. 最小高度树
TiDB与 MySQL 兼容性对比
Run opcua protocol demo on raspberry pie 4B to access kubeedge
Gaussian distribution, linear regression, logistic regression
[QT dot] QT download link
How to check the frequency of memory and the number of memory slots in CPU-Z?
0.0.0.0:x的含义
openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法
JVM常用指令
Proxy-Reflect使用详解
数据库-索引
Assembly language - Wang Shuang Chapter 13 int instruction - Notes