select可以达到微妙级别,你百度下select timer,下面是win下面使用select的例子#include<iostream>#include <iomanip>#include <windows.h>#include <winsock.h>#pragma comment(lib, "ws2_32.lib")using namespace stdint main(){ DWORD dwVersion = 0 WSADATA wsaData DWORD dwErr = 0 dwErr = WSAStartup(MAKEWORD(0x2, 0x2), &wsaData) if (0 != dwErr) {printf("winsock init error!\n")return -1} SOCKET s = socket(AF_INET, SOCK_STREAM, 0) if (INVALID_SOCKET == s) { printf("create a socket error!\n") WSACleanup() return -1 } // 设置为非阻塞 int nRet = 0 u_long lRet = 1 nRet = ioctlsocket(s, FIONBIO, &lRet) if (SOCKET_ERROR == nRet) { printf("set socket error!\n") closesocket(s) WSACleanup() return -1 } int time=0 while( 1 ){ fd_set rfds struct timeval tval FD_ZERO( &rfds ) FD_SET(1,&rfds) tval.tv_sec = 1 /*秒*/ tval.tv_usec =0 /*
微秒, 1秒=10的3次方毫秒=10的6次方微妙*/ int ret =select( 1, &rfds, NULL, NULL, &tval ) if(ret==0 ){ cout<<setfill('0')<<setw(3)<<++time<<" s"<<endl } else {cout<<WSAGetLastError()<<endl break } } WSACleanup() return 0}QTime time
time.start()
while(time.elapsed()>=spinBoxGap->value()*1000)
// 从此段代码可以看出,你是想设定一个时间间隔,执行一次循环time.elapsed(),但是你此处的time未设置间隔。具体设置好像直接start(1000)就可以了,1000单位是毫秒。,即每分钟循环执行1次。
// 给你推荐个方法采用QTimer(定时器)
timer = new QTimer()
// 设置定时器执行间隔
timer->setInterval(spinBoxGap->currentText().toInt() * 1000)
// 然后连接槽函数,执行相关 *** 作即可
connect(timer, SIGNAL(timeout()), this, SLLOT(whatyoudo()))
评论列表(0条)