当前位置:网站首页>[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);
}
边栏推荐
- 【入门】正则表达式基础入门笔记
- Distribution gaussienne, régression linéaire, régression logistique
- 【Cocos Creator 3.5.1】input. Use of on
- EasyExcel:读取Excel数据到List集合中
- Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
- TiDB 中的SQL 基本操作
- 块级元素&行内元素
- IDEA中关于Postfix Completion代码模板的一些设置
- JVM object composition and storage
- Us camera cloud service scheme: designed for lightweight video production scenes
猜你喜欢
JVM类加载机制
多线程基础部分Part2
Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
块级元素&行内元素
0.0.0.0:x的含义
My opinion on test team construction
Formation and release of function stack frame
免费的 SSH 和 Telnet 客户端PuTTY
Software testing year end summary report template
[email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT"/>
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
随机推荐
Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
Software testing year end summary report template
Us camera cloud service scheme: designed for lightweight video production scenes
JVM object composition and storage
[QT notes] simple understanding of QT meta object system
Contents in qlistwidget are not displayed
IDEA一键生成Log日志
Create a basic WDM driver and use MFC to call the driver
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
力扣 179、最大数
【QT小记】QT元对象系统简单认识
TiDB 中的SQL 基本操作
Win 10 如何打开环境变量窗口
机 器 学 习
JVM整体结构解析
【Cocos Creator 3.5.1】event. Use of getbutton()
多线程基础部分Part3
Assembly language - Wang Shuang Chapter 13 int instruction - Notes
【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
LeetCode 0086. Separate linked list