现用Sleep函数来延时,期间如果要关闭应用程序,该怎么办?谢谢!

解决方案 »

  1.   

    用sleep函数延时就是让程序停止响应某段时间,可以改用定时器的方法SetTimer()
      

  2.   

    如果你要在SLEEP期间响应用户界面事件,请不要在主线程中使用SLEEP,开一个线程来等比较好。
    也可以开新线程或其它进程来中止(关闭)本进程。
      

  3.   

    当然,通常最好的方式是主界面来响应用户操作,
    用CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
    开新工作线程来做事,
    这样你就可以用线程调度函数DWORD ResumeThread(
      HANDLE hThread   // identifies thread to restart
    );
    DWORD SuspendThread(
      HANDLE hThread   // handle to the thread
    );
    BOOL TerminateThread(
      HANDLE hThread,    // handle to the thread
      DWORD dwExitCode   // exit code for the thread
    );
    等函数来控制你的工作线程了。这比起用SetTimer设定时器,在OnTimer中检查结果(不用SLEEP)实现,代码更好理解一些。
      

  4.   

    开一个线程去做你想做的事
    这样UI就不会宕掉了------------------
                 May you succeed!
                    --------------------------
      

  5.   

    还有,换一个延时函数:
    Delay(int t)//t延时的时间
    {     MSG msg;
    t+=GetCurrentTime();
    while(GetCurrentTime()<t)
    {
    if(PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
    {TranslateMessage(&msg); DispatchMessage(&msg);}
    }
    }
      

  6.   

    多线程处理, yanxing(初级农民)的方法可以用,但是不太好用,占用CPU大。