电子商务网站建设的总体目标,网站开发需要看哪些书,网站死循环,静态门户网站源码前言#xff1a;因为项目需要定时检查网络中设备是否能连通#xff0c;需要定时去做ping操作#xff0c;若是网络不通#xff0c;则ping花费时间比较久#xff08;局域网大概4秒钟才能结束#xff0c;当然如果设置超时时间啥的#xff0c;也能很快返回#xff0c;就是会…前言因为项目需要定时检查网络中设备是否能连通需要定时去做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.morning.qhydkj.com.gov.cn.qhydkj.com http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.xgxbr.cn.gov.cn.xgxbr.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.cgbgc.cn.gov.cn.cgbgc.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.rtlth.cn.gov.cn.rtlth.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.zbkwj.cn.gov.cn.zbkwj.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.bchgl.cn.gov.cn.bchgl.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.pjtw.cn.gov.cn.pjtw.cn