Qt 定时器放在线程中执行,支持随时开始和停止定时器。

前言:因为项目需要定时检查网络中设备是否能连通,需要定时去做ping操作,若是网络不通,则ping花费时间比较久(局域网大概4秒钟才能结束,当然如果设置超时时间啥的,也能很快返回,就是会报QProcess提前关闭的警告)。此外,我想要在设备连接后才去开始定时ping,要是断开后,则停止定时器,停止ping。网上有很多定时器使用线程的例子,但是没有讲到可以随时停止和开始的那种,本文结合实例,使用线程去执行定时器的定时操作。

一、定义定时器和线程变量。

在头文件中定义

    #include <QTimer>
    #include <QThread>
    #include <QString>
    #include <QProcess>

    /** 监听网线拔出或者wifi断开情况的定时器 ***/
    QTimer *detectNetCablePulloutTimer = NULL;
    /** 执行定时器的线程 **/
    QThread *detectNetCablePulloutTimerThread = NULL;

    /** 开始检测网线拔出时或者wifi断开时的定时器 **/
    void startDetectNetCablePulloutTimer();
    /** 停止定时器 **/
    void stopDetectNetCablePulloutTimer();

private slots:
    /** 检测网络连接情况的槽函数,在线程中执行 **/
    void checkDeviceIsOnline();

二、开始定时器、停止定时器以及销毁定时器代码。

开始定时器代码:

void CurrentXXXClass::startDetectNetCablePulloutTimer()
{
    // 先停止,确保开始之前是停止状态
    stopDetectNetCablePulloutTimer();
    if (detectNetCablePulloutTimer == NULL) {
        detectNetCablePulloutTimerThread = new QThread(this);
        detectNetCablePulloutTimer = new QTimer();
        // 5秒执行一次,局域网断开时,一般耗时4-5秒
        detectNetCablePulloutTimer->setInterval(5000);
		// 定时操作放在线程中执行
        detectNetCablePulloutTimer->moveToThread(detectNetCablePulloutTimerThread);
        // 定时任务放在线程中执行, 必须指定参数-Qt::DirectConnection
        connect(detectNetCablePulloutTimer, &QTimer::timeout, this,
                &CurrentXXXClass::checkDeviceIsOnline, Qt::DirectConnection);
        void (QTimer::*pStartFun)() = &QTimer::start;
        // 线程开始时,调用定时器的开始
        connect(detectNetCablePulloutTimerThread, &QThread::started, detectNetCablePulloutTimer, pStartFun);
        // 线程结束时,调用定时器的结束,所以要停止定时器,只要调用线程的quit方法,
        // 同时防止提示:定时器停止和开始不在同一线程错误
        connect(detectNetCablePulloutTimerThread, &QThread::finished, detectNetCablePulloutTimer, &QTimer::stop);
    }
    detectNetCablePulloutTimerThread->start();
}

停止定时器代码:

void CurrentXXXClass::stopDetectNetCablePulloutTimer()
{
    if (detectNetCablePulloutTimerThread != NULL) {
        // 会调用定时器的stop方法
        detectNetCablePulloutTimerThread->quit();
    }
}

 销毁定时器,在析构函数中执行:

    //销毁指针变量-注意先后顺序
    if (detectNetCablePulloutTimerThread != NULL) {
        detectNetCablePulloutTimerThread->quit();
        detectNetCablePulloutTimerThread->wait();
        delete detectNetCablePulloutTimerThread;
        detectNetCablePulloutTimerThread = nullptr;
    }
    if (detectNetCablePulloutTimer != NULL) {
        delete detectNetCablePulloutTimer;
        detectNetCablePulloutTimer = nullptr;
    }

三、在槽函数中执行耗时操作。

此槽函数是在子线程中执行,若是执行完后要回到主线程执行某操作,则必须通过发送一个信号方式才能达到效果。

void CurrentXXXClass::checkDeviceIsOnline()
{
	QString ip = "192.168.1.1";
    // 若是timer使用了线程,则此函数在子线程中执行,执行比较耗时的操作
	QProcess pingProcess;
	// 不设置-w参数
    QString strArg = "ping " + ip + " -n 1 -i 2";
	// qt 6 开始命令的方法
    pingProcess.startCommand(strArg, QIODevice::ReadOnly);
	// 不带等待时间参数
    pingProcess.waitForReadyRead();
	// 不带等待时间参数
    pingProcess.waitForFinished();
    QString p_stdout = QString::fromLocal8Bit(pingProcess.readAllStandardOutput());
    bool bPingSuccess = false;
    // 通过特殊字符串进行判断ping是否成功
    if (p_stdout.contains("TTL=")) {
		// 网络连通
        bPingSuccess = true;
    } else {
		// 网络不通
        bPingSuccess = false;
    }
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Qt定时器线程都是常用的实现异步操作的方法,它们各有优缺点。 定时器的优点: 1. 简单易用:使用QTimer类可以方便地创建和操作定时器,不需要复杂的线程管理代码。 2. 轻量级:定时器不需要创建新的线程,可以在主线程执行,减少了系统资源的消耗。 3. 稳定性高:定时器执行周期稳定,不容易出现死锁和资源竞争等问题。 定时器的缺点: 1. 精度较低:定时器的精度受到Qt事件循环的影响,无法保证精确到毫秒级别。 2. 不适用于长时间操作:如果需要执行长时间的操作,定时器会阻塞事件循环,导致界面卡顿。 线程的优点: 1. 可以执行长时间操作:线程可以在后台执行长时间的操作,不会影响界面的响应。 2. 提高程序的并发性:线程可以并发执行多个任务,提高了程序的效率。 3. 稳定性高:线程执行不会受到Qt事件循环的影响,稳定性较高。 线程的缺点: 1. 管理复杂:线程需要手动创建、启动和停止,需要注意线程同步和资源竞争等问题。 2. 容易出现死锁和资源竞争问题:线程间共享数据时需要进行同步,不注意可能会出现死锁和资源竞争等问题。 综上所述,定时器适用于执行简单的、周期性的操作,而线程适用于执行长时间的、复杂的操作。在选择使用哪种方法时,需要根据具体的需求和实际情况进行选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值