当前位置: 首页 > news >正文

重庆市建设教育培训网win7优化大师

重庆市建设教育培训网,win7优化大师,北海 网站建设 公司,网站建设中国十强前言:因为项目需要定时检查网络中设备是否能连通,需要定时去做ping操作,若是网络不通,则ping花费时间比较久(局域网大概4秒钟才能结束,当然如果设置超时时间啥的,也能很快返回,就是会…

前言:因为项目需要定时检查网络中设备是否能连通,需要定时去做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::DirectConnectionconnect(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;}
}

http://www.tj-hxxt.cn/news/47192.html

相关文章:

  • 做公司网站需要注意什么企业网络营销青岛
  • wordpress釆集插件破解seo百家论坛
  • 网站 文件服务器网站关键词排名查询
  • 网站管理系统设置潍坊网站排名提升
  • 中卫网站网站建设扬中网站制作
  • 做网站还有流量么互联网推广招聘
  • 简述网站建设步骤恶意点击软件哪个好
  • 成都哪家公司做网站产品策划方案怎么做
  • 网站的在线支付怎么做怎么推广引流客户
  • 如何查看网站是哪家公司做的营销模式100个经典案例
  • 西宁做网站今日疫情最新情况
  • 汉川网站制作企业网站推广方法实验报告
  • 博主回应网络热梗谷歌搜索广告优化
  • 一个网站收录很高 但外链很少是什么原因网络营销的内容
  • 药品行业做网站武汉网站建设方案优化
  • 电话怎么做网站推广b2b电子商务平台有哪些
  • 电池外贸一般在哪些网站做广告主资源哪里找
  • 基金会网站建设软文代发布
  • 怎样做外贸网站推广病毒式营销
  • discu论坛网站模板杭州seo价格
  • 图表生成网站百度文库个人登录
  • 开通招聘网站如何做分录市场调研报告范文大全
  • 企业定制网站开发维护合同百度竞价推广点击软件
  • 企业手机网站建设案例百度图片搜索引擎入口
  • 昆明网站建设 技术支持变现流量推广app
  • 成都网站建设哪家公司靠谱南京百度seo公司
  • 软件开发设计文档示例seo一般包括哪些内容
  • 深圳精品网站设计百度下载安装到手机
  • 学校网站的建设目标是什么意思开封网络推广哪家好
  • 代理加盟微信网站建设武汉seo工厂