一 MFC中定時器的使用
1.1、使用場景
定時讀寫數(shù)據(jù),或者定時刷新界面,更新數(shù)據(jù)和狀態(tài)。
1.2、使用
(1)編寫OnTimer函數(shù)
void CMultipleCameraDlg::OnTimer(UINT_PTR nIDEvent) //定時讀取數(shù)據(jù)
{
if(nIDEvent == WM_TIMER_GRAB_INFO)
{
int nRet = 0;
CString pos;
int nPos = 0;
pos.Format(_T("%d"), nPos);
unsigned int nLostFrame = 0;
unsigned int nFrameCount = 0;
MV_MATCH_INFO_NET_DETECT stMatchInfoNetDetect = {0};
MV_MATCH_INFO_USB_DETECT stMatchInfoUSBDetect = {0};
for (int i = 0; i < m_nSelectDeviceNum; i++)
{
MV_CC_DEVICE_INFO stDevInfo = {0};
m_pcMyCamera[i]->GetDeviceInfo(&stDevInfo);
if (stDevInfo.nTLayerType == MV_GIGE_DEVICE)
{
nRet = m_pcMyCamera[i]->GetGevAllMatchInfo(&stMatchInfoNetDetect);
nLostFrame = stMatchInfoNetDetect.nLostFrameCount;
nFrameCount = stMatchInfoNetDetect.nNetRecvFrameCount;
}
else if (stDevInfo.nTLayerType == MV_USB_DEVICE)
{
nRet = m_pcMyCamera[i]->GetU3VAllMatchInfo(&stMatchInfoUSBDetect);
nLostFrame = stMatchInfoUSBDetect.nErrorFrameCount;
nFrameCount = stMatchInfoUSBDetect.nReceivedFrameCount;
}
else
{
return;
}
if (MV_OK == nRet)
{
switch(i)
{
case 0:
{
m_nFrameCount1Edit = nFrameCount;
m_nLostFrame1Edit = nLostFrame;
}
break;
case 1:
{
m_nFrameCount2Edit = nFrameCount;
m_nLostFrame2Edit = nLostFrame;
}
break;
case 2:
{
m_nFrameCount3Edit = nFrameCount;
m_nLostFrame3Edit = nLostFrame;
}
break;
case 3:
{
m_nFrameCount4Edit = nFrameCount;
m_nLostFrame4Edit = nLostFrame;
}
break;
default:
break;
}
UpdateData(FALSE); //界面數(shù)據(jù)和自定義變量進行同步
}
}
}
CDialog::OnTimer(nIDEvent);
}
(2)啟動定時器
SetTimer(1,1000,NULL); //參數(shù):定時器標(biāo)號,定時時間(ms)。啟動定時器1,每隔1s刷新一次
(3)關(guān)閉定時器
KillTimer(1); //關(guān)定時器1
二 QT中定時器使用
2.1 簡述
QTimer類提供了重復(fù)和單次觸發(fā)信號的定時器。
2.2 使用
(1)定義
QTimer *timer; // 定義的定時器
(2)使用
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(function()));
// 調(diào)用自定義的槽函數(shù)
timer->start(100);
(3)結(jié)束采集
timer->stop();