当前位置:网站首页>[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);
}
边栏推荐
- 使用CSDN 开发云搭建导航网站
- IDEA中关于Postfix Completion代码模板的一些设置
- Quick personal site building guide using WordPress
- Configuring the help class iconfiguration in C # NETCORE
- 【Cocos Creator 3.5.1】event.getButton()的使用
- Configuration of vscode korofileheader
- 【QT小作】使用结构体数据生成读写配置文件代码
- 427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
- Spark 之 built-in functions
- Spark 之 Projection
猜你喜欢
随机推荐
多线程基础部分Part 1
【QT小记】QT元对象系统简单认识
The form verifies the variables bound to the V-model, and the solution to invalid verification
汇编语言-王爽 第13章 int指令-笔记
G1 and ZGC garbage collector
Senior [Software Test Engineer] learning route and necessary knowledge points
JVM的垃圾回收机制
Win 10 如何打开环境变量窗口
693. alternate bit binary number
使用CSDN 开发云搭建导航网站
【Cocos Creator 3.5.1】event. Use of getbutton()
函数栈帧的形成与释放
【养成系】常用正则表达式
Thinking technology: how to solve the dilemma in work and life?
Active learning
Redis4.0新特性-主动内存碎片整理
WebRTC系列-網絡傳輸之7-ICE補充之提名(nomination)與ICE_Model
汇编语言-王爽 第11章 标志寄存器-笔记
[getting started] regular expression Basics
数据库-索引









