我想要一个能控制一个函数,能让这个函数每5分钟执行一次的完整代码?
被控制函数名用aaaaa代替,谢谢!!紧急需要谢谢!!!能加一些注释更好了谢谢!!!

解决方案 »

  1.   

    那还不好办啊。做个定时器不就完了。没5分钟触发一次,在OnTimer函数中就调用这个函数。可以吧。
      

  2.   

    UINT Dothread(LPVOID lpParam)
    {
    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);
    }
      

  3.   

    个人认为用定时器就可以了,难道楼主的程序还有什么玄机吗?
    实在不行可以开个线程,用while循环试试
    while (true)
    {
        ::Sleep(5000);
        aaaaa;
        if (xxx) //设置一个终止循环的标记,退出循环
        {
            break; //or return;
        }
    }
      

  4.   

    在 windows 的控制台下面怎么用定时器??
      

  5.   

    你自己去看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. 
      

  6.   

    每5分钟触发一次,在OnTimer函数中就调用这个函数
      

  7.   

    控制台下面没有窗口,所以不能用计时器的方法,可以用上面那位仁兄写的Sleep()方法,注意Sleep()大写,小写的好像只有TurboC可以^_^