写了一个for循环,处理一些文件;现在想弄一个(或2个)按钮,单击“暂停”按钮,for循环暂停;单击“继续”按钮,for循环从上次暂停的位置继续做。
  请问各位大侠,应该怎么实现呀?用CWinThread的SuspendThread等方法吗?

解决方案 »

  1.   

    当然用SuspendThread和ResumeThread 也可以,但psdk有这样的建议:This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
      

  2.   

    能不能给个具体的小程序?谢谢了。以前没用过WaitForSingleObject和CEvent。
      

  3.   

    The WaitForSingleObject function returns when the specified object is in the signaled state or the time-out interval elapses.To enter an alertable wait state, use the WaitForSingleObjectEx function. To wait for multiple objects, use the WaitForMultipleObjects.
    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.Constructs a named or unnamed CEvent object. 
    CEvent(
       BOOL bInitiallyOwn = FALSE,
       BOOL bManualReset = FALSE,
       LPCTSTR lpszName = NULL,
       LPSECURITY_ATTRIBUTES lpsaAttribute = NULL 
    );
     
    Parameters
    bInitiallyOwn
    If TRUE, the thread for the CMultilock or CSingleLock object is enabled. Otherwise, all threads wanting to access the resource must wait.bManualReset
    If TRUE, specifies that the event object is a manual event, otherwise the event object is an automatic event.lpszName
    Name of the CEvent object. Must be supplied if the object will be used across process boundaries. If the name matches an existing event, the constructor builds a new CEvent object which references the event of that name. If the name matches an existing synchronization object that is not an event, the construction will fail. If NULL, the name will be null. lpsaAttribute
    Security attributes for the event object. For a full description of this structure, see SECURITY_ATTRIBUTES in the Platform SDK.例子就不给出了,psdk和msdn上多如牛毛,自己查一下吧