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

家居网站 模板金华百度seo

家居网站 模板,金华百度seo,楼市南京做凶宅的网站,内蒙古自治区住房和城乡建设厅网站前言:因为项目需要定时检查网络中设备是否能连通,需要定时去做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.fp688.cn/news/154617.html

相关文章:

  • wordpress 短代码 if is single站长工具seo推广秒收录
  • 科技公司网站版面设计网页设计效果图及代码
  • 网站后台编辑器无法显示关键词优化公司哪家好
  • 本地企业网站建设模板潍坊网站建设seo
  • 河源市建设网站北京seo多少钱
  • 大连网络营销网站建站系统源码
  • 重庆网站建设网领科技昆明网络推广公司排名
  • 未支付网站建设挂哪个科目虎扑体育网体育
  • 什么专业学做网站如何在手机上制作网站
  • wordpress页头导航类目没有链接济南优化网页
  • 在国外服务器上做网站项目如何赚钱搜索引擎优化的要点
  • 企业网站备案信息查询系统宁波网站建设公司
  • 湘潭学校网站建设 磐石网络专注抚顺网络推广
  • 医疗网站设计网站重庆做seo外包的
  • 网站备案 优帮云google官网注册
  • dreamweaver怎么做网站吴中seo页面优化推广
  • 做淘宝优惠网站谷歌关键词推广怎么做
  • web浏览器什么意思重庆网页优化seo公司
  • 用表格做网站教程市场推广方案和思路
  • 专业做俄语网站建设司免费隐私网站推广
  • 自己如何建企业网站网页设计培训教程
  • 东莞模板网站制作哪家好html友情链接
  • 北京自助建站系统西安高端网站建设公司
  • 国内室内设计网站推荐网络营销推广主要做什么
  • 建设信用卡申请进度查询官方网站培训机构哪家好
  • 网站做seo多少钱怎么在百度制作自己的网站
  • 更改域名代理商对网站有影响吗百度一下子就知道了
  • 网站建设的常见问题无锡整站百度快照优化
  • 做设计的有什么网站seo网站优化方案案例
  • 网站云主机嘉兴seo排名外包