我用Sleep做主程序中等,在Sleep前面我开了一个线程。为什么在Sleep的过程中,线程也不干活了??为什么?如果让线程干活,主程序等,用什么方法实现?

解决方案 »

  1.   

    用WaitForSingleObject();DWORD WaitForSingleObject(
      HANDLE hHandle,
      DWORD dwMilliseconds
    );Parameters
    hHandle 
    [in] Handle to the object. For a list of the object types whose handles can be specified, see the following Res section. 
    If this handle is closed while the wait is still pending, the function's behavior is undefined.The handle must have the SYNCHRONIZE access right. For more information, see Standard Access Rights.dwMilliseconds 
    [in] Time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses. 
    Return Values
    If the function succeeds, the return value indicates the event that caused the function to return. It can be one of the following values.Return Code Description:WAIT_ABANDONED The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled. WAIT_OBJECT_0 The state of the specified object is signaled. WAIT_TIMEOUT The time-out interval elapsed, and the object's state is nonsignaled. 
      

  2.   

    reference by MSDN..You have to be careful when using Sleep and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock.
      

  3.   

    在主线程中,尽量不要用Sleep去等待啊..
    可能就是你的工作线程创建后是挂起状态的,一般设置一个全局的同步变量, 主线程再另外开启一个线程,去检测这个同步变量的值变化以确定工作线程是否完成