var BatchTestDealEvent: THandle ;BatchTestDealEvent := CreateEvent(nil, False, False, 'BatchTestBeginEndEvent') ;  for i := 0 to 4 do
    begin
    .......
    WaitForSingleObject(BatchTestDealEvent, 30000) ;
    .......
    end ; //of for在另一个线程中执行SetEvent(BatchTestDealEvent) ;问题:
WaitForSingleObject执行时是同步还是异步?
它执行大概需要多长时间?我用起来发现,WaitForSingleObject把BatchTestDealEvent设为无信号状态好像需要一段时间,而且是异步的,也就是这次WaitForSingleObject还没有执行完,下一个循环已经到WaitForSingleObject了,所以BatchTestDealEvent还是有信号的
请有经验者回答,up有分

解决方案 »

  1.   

    1,同步,执行该函数的时候第而个参数就是停留的时间;
    2,在停留的这段时间,如果一检测到状态发生变化,就会返回的;
    Identifies the object. For a list of the object types whose handles can be specified, see the following Res section. 
    Windows NT: The handle must have SYNCHRONIZE access. For more information, see Access Masks and Access Rights. dwMillisecondsSpecifies the 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 ValuesIf the function succeeds, the return value indicates the event that caused the function to return.
    If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError. 
    The return value on success is one of the following values: Value Meaning
    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.
     ResThe WaitForSingleObject function checks the current state of the specified object. If the object's state is nonsignaled, the calling thread enters an efficient wait state. The thread consumes very little processor time while waiting for the object state to become signaled or the time-out interval to elapse.
    Before returning, a wait function modifies the state of some types of synchronization objects. Modification occurs only for the object or objects whose signaled state caused the function to return. For example, the count of a semaphore object is decreased by one. The WaitForSingleObject function can wait for the following objects: Object Description
    Change notification The FindFirstChangeNotification function returns the handle. A change notification object's state is signaled when a specified type of change occurs within a specified directory or directory tree.
    Console input The handle is returned by the CreateFile function when the CONIN$ value is specified, or by the GetStdHandle function. The object's state is signaled when there is unread input in the console's input buffer, and it is nonsignaled when the input buffer is empty.
    Event The CreateEvent or OpenEvent function returns the handle. An event object's state is set explicitly to signaled by the SetEvent or PulseEvent function. A manual-reset event object's state must be reset explicitly to nonsignaled by the ResetEvent function. For an auto-reset event object, the wait function resets the object's state to nonsignaled before returning. Event objects are also used in overlapped operations, in which the state is set by the system.
    Mutex The CreateMutex or OpenMutex function returns the handle. A mutex object's state is signaled when it is not owned by any thread. The wait function requests ownership of the mutex for the calling thread, changing the mutex's state to nonsignaled when ownership is granted.
    Process The CreateProcess or OpenProcess function returns the handle. A process object's state is signaled when the process terminates.
    Semaphore The CreateSemaphore or OpenSemaphore function returns the handle. A semaphore object maintains a count between zero and some maximum value. Its state is signaled when its count is greater than zero and nonsignaled when its count is zero. If the current state is signaled, the wait function decreases the count by one.
    Thread The CreateProcess, CreateThread, or CreateRemoteThread function returns the handle. A thread object's state is signaled when the thread terminates.
    Timer The CreateWaitableTimer or OpenWaitableTimer function returns the handle. Activate the timer by calling the SetWaitableTimer function. The state of an active timer is signaled when it reaches its due time. You can deactivate the timer by calling the CancelWaitableTimer function. The state of an active timer is signaled when it reaches its due time. You can deactivate the timer by calling the CancelWaitableTimer function.
      

  2.   

    WaitForSingleObject把BatchTestDealEvent设为无信号状态
    为 异步的 不同的事件拥有着不同状态以及级别的信号 
      

  3.   

    应该是同步的,我没有深入研究,如果waitforsingleobject(的第二个参数设为 INFINITE 就不会出现你说的问题了吧
      

  4.   

    应该是同步的,如果waitforsingleobject的第二个参数设为 INFINITE 永远等待直到EVENT事件SIGNALED触发就返回
      

  5.   

    不是标准答案,仅供参考。DWORD WaitForSingleObject(    HANDLE hHandle, // handle of object to wait for    DWORD dwMilliseconds  // time-out interval in milliseconds   );参数:  hHandle :等待对象句柄。  dwMillSeconds: 指定以毫秒为单位的超时间隔。返回值:  WAIT_ABANDONED: 指定对象是互斥对象,在线程被终止前,线程没有释放互斥对象。互斥对象的所属关系被授予调用线程,并且该互斥对象被置为非信号态。  WAIT_OBJECT_0:  指定对象的状态被置为信号状态。  WAIT_TIMEOUT:   超时,并且对象的状态为非信号态。  WAIT_FAILED:    调用失败。
      

  6.   

    当线程调用该函数时,第一个参数h O b j e c t标识一个能够支持被通知/未通知的内核对象。第二个参数d w M i l l i s e c o n d s允许该线程指明,为了等待该对象变为已通知状态,它将等待多长时间。
    第二个参数告诉系统,调用线程愿意永远等待下去(无限时间量),直到该进程终止运行。
    通常情况下, I N F I N I T E是作为第二个参数传递给Wa i t F o r S i n g l e O b j e c t的,不过也可以传递任何一个值(以毫秒计算)。顺便说一下, I N F I N I T E已经定义为0 x F F F F F F F F(或-1)。当然,传递I N F I N I T E有些危险。如果对象永远不变为已通知状态,那么调用线程永远不会被唤醒,它将永远处于死锁状态,不过,它不会浪费宝贵的C P U时
      

  7.   

    ResThe WaitForSingleObject function checks the current state of the specified object. If the object's state is nonsignaled, the calling thread enters an efficient wait state. The thread consumes very little processor time while waiting for the object state to become signaled or the time-out interval to elapse.
    Before returning, a wait function modifies the state of some types of synchronization objects. 
    ~~~~~~~~~~~~~~~~
    Modification occurs only for the object or objects whose signaled state caused the function to return. For example, the count of a semaphore object is decreased by one. The WaitForSingleObject function can wait for the following objects:
      

  8.   

    我在VC和delphi中都用过这个函数。
    你的问题我也碰到过,很有可能是另外的线程在这个线程执行WaitForSingleObject时,调用了
    SetEvent(BatchTestDealEvent),因为线程间肯定是异步。
    我看你写的程序转换频率很高,WaitForSingleObject和SetEvent(BatchTestDealEvent) 函数转换速度可能慢了,才出现你的问题,所以我建议你用同步区或互斥,它的转换效率很高。