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有分
begin
.......
WaitForSingleObject(BatchTestDealEvent, 30000) ;
.......
end ; //of for在另一个线程中执行SetEvent(BatchTestDealEvent) ;问题:
WaitForSingleObject执行时是同步还是异步?
它执行大概需要多长时间?我用起来发现,WaitForSingleObject把BatchTestDealEvent设为无信号状态好像需要一段时间,而且是异步的,也就是这次WaitForSingleObject还没有执行完,下一个循环已经到WaitForSingleObject了,所以BatchTestDealEvent还是有信号的
请有经验者回答,up有分
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.
为 异步的 不同的事件拥有着不同状态以及级别的信号
第二个参数告诉系统,调用线程愿意永远等待下去(无限时间量),直到该进程终止运行。
通常情况下, 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时
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:
你的问题我也碰到过,很有可能是另外的线程在这个线程执行WaitForSingleObject时,调用了
SetEvent(BatchTestDealEvent),因为线程间肯定是异步。
我看你写的程序转换频率很高,WaitForSingleObject和SetEvent(BatchTestDealEvent) 函数转换速度可能慢了,才出现你的问题,所以我建议你用同步区或互斥,它的转换效率很高。