个人认为用定时器就可以了,难道楼主的程序还有什么玄机吗? 实在不行可以开个线程,用while循环试试 while (true) { ::Sleep(5000); aaaaa; if (xxx) //设置一个终止循环的标记,退出循环 { break; //or return; } }
在 windows 的控制台下面怎么用定时器??
你自己去看msdn吧,控制台的话要自己写回调函数UINT_PTR SetTimer(HWND hWnd, UINT_PTR nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc ); ParametershWnd [in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored. nIDEvent [in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. uElapse [in] Specifies the time-out value, in milliseconds. lpTimerFunc [in] Pointer to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc. If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message's MSG structure contains the value of the hWnd parameter.
{
CMyXXX *pXXX = (CMyXXX *)lpParam;
while(!g_bStop)
{
int nStart = GetTickCount();
pXXX->aaaaaa(); // 为了线程推出,使用自己的计时器
while(!g_bStop)
{
Sleep(200);
int nEnd = GetTickCount();
if((nEnd - nStart) >= 5*60*1000)
break ;
}
}
}void CMyXXX::StartThread()
{
g_bStop = FALSE;
m_pWinThread = AfxBeginThread(Dothread, (LPVOID)this);
}void CMyXXX:StopThread()
{
g_bStop = TRUE;
if(m_pWinThread != NULL)
WaitForSingleObject(m_pWinThread->m_hThread, 2000);
}
实在不行可以开个线程,用while循环试试
while (true)
{
::Sleep(5000);
aaaaa;
if (xxx) //设置一个终止循环的标记,退出循环
{
break; //or return;
}
}
UINT_PTR nIDEvent,
UINT uElapse,
TIMERPROC lpTimerFunc
);
ParametershWnd
[in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.
nIDEvent
[in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored. If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored.
uElapse
[in] Specifies the time-out value, in milliseconds.
lpTimerFunc
[in] Pointer to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc. If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message's MSG structure contains the value of the hWnd parameter.