Monitor.Wait 方法

定义

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock.

重载

Wait(Object, Int32, Boolean)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock. 如果已用指定的超时时间间隔,则线程进入就绪队列。If the specified time-out interval elapses, the thread enters the ready queue. 此方法还指定是否在等待之前退出上下文的同步域(如果在同步上下文中)然后重新获取该同步域。This method also specifies whether the synchronization domain for the context (if in a synchronized context) is exited before the wait and reacquired afterward.

Wait(Object)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock.

Wait(Object, Int32)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock. 如果已用指定的超时时间间隔,则线程进入就绪队列。If the specified time-out interval elapses, the thread enters the ready queue.

Wait(Object, TimeSpan)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock. 如果已用指定的超时时间间隔,则线程进入就绪队列。If the specified time-out interval elapses, the thread enters the ready queue.

Wait(Object, TimeSpan, Boolean)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock. 如果已用指定的超时时间间隔,则线程进入就绪队列。If the specified time-out interval elapses, the thread enters the ready queue. 可以在等待之前退出同步上下文的同步域,随后重新获取该域。Optionally exits the synchronization domain for the synchronized context before the wait and reacquires the domain afterward.

Wait(Object, Int32, Boolean)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock. 如果已用指定的超时时间间隔,则线程进入就绪队列。If the specified time-out interval elapses, the thread enters the ready queue. 此方法还指定是否在等待之前退出上下文的同步域(如果在同步上下文中)然后重新获取该同步域。This method also specifies whether the synchronization domain for the context (if in a synchronized context) is exited before the wait and reacquired afterward.

public:
 static bool Wait(System::Object ^ obj, int millisecondsTimeout, bool exitContext);
public static bool Wait (object obj, int millisecondsTimeout, bool exitContext);
static member Wait : obj * int * bool -> bool
Public Shared Function Wait (obj As Object, millisecondsTimeout As Integer, exitContext As Boolean) As Boolean

参数

obj
Object

要在其上等待的对象。The object on which to wait.

millisecondsTimeout
Int32

线程进入就绪队列之前等待的毫秒数。The number of milliseconds to wait before the thread enters the ready queue.

exitContext
Boolean

如果在等待前退出并重新获取上下文的同步域(如果在同步上下文中),则为 true;否则为 falsetrue to exit and reacquire the synchronization domain for the context (if in a synchronized context) before the wait; otherwise, false.

返回

Boolean

如果在指定的时间过期之前重新获取该锁,则为 true;如果在指定的时间过期之后重新获取该锁,则为 falsetrue if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. 此方法只有在重新获取该锁后才会返回。The method does not return until the lock is reacquired.

例外

obj 参数为 nullThe obj parameter is null.

Wait 不是从同步的代码块中调用的。Wait is not invoked from within a synchronized block of code.

调用 Wait 的线程稍后从等待状态中断。The thread that invokes Wait is later interrupted from the waiting state. 另一线程调用此线程的 Interrupt() 方法时将发生这种情况。This happens when another thread calls this thread's Interrupt() method.

millisecondsTimeout 参数值为负且不等于 InfiniteThe value of the millisecondsTimeout parameter is negative, and is not equal to Infinite.

注解

此方法在重新获取该域参数的排他锁之前不会返回 objThis method does not return until it reacquires an exclusive lock on the obj parameter.

当前拥有指定对象的锁的线程调用此方法以释放该对象,以便其他线程可以访问它。The thread that currently owns the lock on the specified object invokes this method in order to release the object so that another thread can access it. 等待重新获取该锁时,调用方被阻止。The caller is blocked while waiting to reacquire the lock. 当调用方需要等待作为其他线程操作的结果而发生的状态更改时,将调用此方法。This method is called when the caller needs to wait for a state change that will occur as a result of another thread's operations.

超时可确保当另一个线程释放该锁,而不首先调用或方法时,当前线程不会无限期地阻止 Pulse PulseAllThe time-out ensures that the current thread does not block indefinitely if another thread releases the lock without first calling the Pulse or PulseAll method. 它还会将线程移动到就绪队列,绕过等待队列中的其他线程,以便它可以更快地重新获取锁定。It also moves the thread to the ready queue, bypassing other threads ahead of it in the wait queue, so that it can reacquire the lock sooner. 线程可以测试方法的返回值 Wait ,以确定它是否在超时前重新获取该锁。线程可以计算导致其进入等待状态的条件,并在必要时 Wait 再次调用方法。The thread can test the return value of the Wait method to determine whether it reacquired the lock prior to the time-out. The thread can evaluate the conditions that caused it to enter the wait, and if necessary call the Wait method again.

当线程调用时 Wait ,它会释放该锁并进入等待队列。When a thread calls Wait, it releases the lock and enters the waiting queue. 此时,准备就绪的队列中的下一个线程 (如果允许一个) 控制该锁。At this point, the next thread in the ready queue (if there is one) is allowed to take control of the lock. 调用的线程将 Wait 保留在等待队列中,直到持有锁的线程调用 PulseAll ,或者它是队列中的下一线程以及持有锁的线程调用 PulseThe thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes PulseAll, or it is the next in the queue and a thread that holds the lock invokes Pulse. 但是,如果在 millisecondsTimeout 另一个线程调用此对象的 PulsePulseAll 方法之前已过,则原始线程将移到就绪队列,以便重新获取锁定。However, if millisecondsTimeout elapses before another thread invokes this object's Pulse or PulseAll method, the original thread is moved to the ready queue in order to regain the lock.

备注

如果 InfinitemillisecondsTimeout 参数指定,则此方法会无限期地阻止,除非锁的持有方调用 PulsePulseAllIf Infinite is specified for the millisecondsTimeout parameter, this method blocks indefinitely unless the holder of the lock calls Pulse or PulseAll. 如果 millisecondsTimeout 等于0,则调用的线程将 Wait 释放该锁,然后立即进入就绪队列以便重新获取锁。If millisecondsTimeout equals 0, the thread that calls Wait releases the lock and then immediately enters the ready queue in order to regain the lock.

Wait无论 Enter 为指定的对象调用了多少次,调用方都将执行一次。The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. 从概念上讲,此 Wait 方法存储调用方 Enter 在对象上调用的次数,并按 Exit 需多次调用以完全释放锁定的对象。Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit as many times as necessary to fully release the locked object. 然后,调用方在等待重新获取对象时阻止。The caller then blocks while waiting to reacquire the object. 当调用方重新获取该域锁定时,系统会根据 Enter 需要多次调用以还原调用方的已保存 Enter 计数。When the caller reacquires the lock, the system calls Enter as many times as necessary to restore the saved Enter count for the caller. 调用 Wait 只释放指定对象的锁; 如果调用方是其他对象的锁的所有者,则不释放这些锁。Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

备注

同步的对象包含多个引用,包括对当前持有锁的线程的引用、对就绪队列的引用、包含准备好获取锁的线程的引用以及对等待队列的引用(其中包含等待对象状态更改通知的线程)。A synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

Pulse PulseAll Wait 必须从同步的代码块中调用、和方法。The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

方法的备注 Pulse 说明 Pulse 了在没有线程等待时调用时所发生的情况。The remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.

有关退出上下文的说明Notes on Exiting the Context

exitContext除非 Wait 从非默认托管上下文内调用方法,否则参数不起作用。TheexitContext parameter has no effect unless the Wait method is called from inside a nondefault managed context. 如果你的线程在对从派生的类的实例的调用中,则可能会发生这种情况 ContextBoundObjectThis can happen if your thread is inside a call to an instance of a class derived from ContextBoundObject. 即使当前正在对不是从派生的类 ContextBoundObject (如)执行方法,如果在 String ContextBoundObject 当前应用程序域中的堆栈上,也可以在非默认上下文中执行。Even if you are currently executing a method on a class that is not derived from ContextBoundObject, like String, you can be in a nondefault context if a ContextBoundObject is on your stack in the current application domain.

当你的代码在非默认上下文中执行时,为指定将 true exitContext 导致线程退出非默认的托管上下文 (也就是说,在执行方法前转换为默认上下文) WaitWhen your code is executing in a nondefault context, specifying true for exitContext causes the thread to exit the nondefault managed context (that is, to transition to the default context) before executing the Wait method. 此方法在调用方法完成后返回到原始的非默认上下文 WaitIt returns to the original nondefault context after the call to the Wait method completes.

当上下文绑定类应用了属性时,这会很有用 SynchronizationAttributeThis can be useful when the context-bound class has the SynchronizationAttribute attribute applied. 在这种情况下,对类成员的所有调用都将自动同步,同步域是类的整个代码正文。In that case, all calls to members of the class are automatically synchronized, and the synchronization domain is the entire body of code for the class. 如果成员的调用堆栈中的代码调用 Wait 方法并 true 为指定 exitContext ,则线程将退出同步域,从而允许在对对象的任何成员的调用上被阻止的线程继续。If code in the call stack of a member calls the Wait method and specifies true for exitContext, the thread exits the synchronization domain, allowing a thread that is blocked on a call to any member of the object to proceed. Wait 方法返回时,进行调用的线程必须等待重新进入同步域。When the Wait method returns, the thread that made the call must wait to reenter the synchronization domain.

另请参阅

适用于

产品 已引入

Wait(Object)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock.

public:
 static bool Wait(System::Object ^ obj);
public static bool Wait (object obj);
static member Wait : obj -> bool
Public Shared Function Wait (obj As Object) As Boolean

参数

obj
Object

要在其上等待的对象。The object on which to wait.

返回

Boolean

如果调用由于调用方重新获取了指定对象的锁而返回,则为 truetrue if the call returned because the caller reacquired the lock for the specified object. 如果未重新获取该锁,则此方法不会返回。This method does not return if the lock is not reacquired.

例外

obj 参数为 nullThe obj parameter is null.

调用线程不拥有指定对象的锁。The calling thread does not own the lock for the specified object.

调用 Wait 的线程稍后从等待状态中断。The thread that invokes Wait is later interrupted from the waiting state. 另一线程调用此线程的 Interrupt() 方法时将发生这种情况。This happens when another thread calls this thread's Interrupt() method.

注解

当前拥有指定对象的锁的线程调用此方法以释放该对象,以便其他线程可以访问它。The thread that currently owns the lock on the specified object invokes this method in order to release the object so that another thread can access it. 等待重新获取该锁时,调用方被阻止。The caller is blocked while waiting to reacquire the lock. 当调用方需要等待作为其他线程操作的结果而发生的状态更改时,将调用此方法。This method is called when the caller needs to wait for a state change that will occur as a result of another thread's operations.

当线程调用时 Wait ,它会释放对象的锁,并进入该对象的等待队列。When a thread calls Wait, it releases the lock on the object and enters the object's waiting queue. 如果有一个) 获取了该锁并且独占使用了该对象,则该对象的 "就绪" 队列中的下一个线程 (。The next thread in the object's ready queue (if there is one) acquires the lock and has exclusive use of the object. 调用的所有线程 Wait 都将保留在等待队列中,直到接收到 Pulse PulseAll 由锁的所有者发送的或发出的信号。All threads that call Wait remain in the waiting queue until they receive a signal from Pulse or PulseAll, sent by the owner of the lock. 如果 Pulse 发送了,则只影响处于等待队列开头的线程。If Pulse is sent, only the thread at the head of the waiting queue is affected. 如果 PulseAll 发送,则会影响正在等待对象的所有线程。If PulseAll is sent, all threads that are waiting for the object are affected. 收到信号后,一个或多个线程将离开等待队列,并进入就绪队列。When the signal is received, one or more threads leave the waiting queue and enter the ready queue. 允许队列中的线程重新获取该锁。A thread in the ready queue is permitted to reacquire the lock.

当调用线程重新获取该域对象上的锁时,此方法返回。This method returns when the calling thread reacquires the lock on the object. 请注意,如果锁定的持有者不调用或,则此方法会无限期阻止 Pulse PulseAllNote that this method blocks indefinitely if the holder of the lock does not call Pulse or PulseAll.

Wait无论 Enter 为指定的对象调用了多少次,调用方都将执行一次。The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. 从概念上讲,此 Wait 方法存储调用方 Enter 在对象上调用的次数,并按 Exit 需多次调用以完全释放锁定的对象。Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit as many times as necessary to fully release the locked object. 然后,调用方在等待重新获取对象时阻止。The caller then blocks while waiting to reacquire the object. 当调用方重新获取该域锁定时,系统会根据 Enter 需要多次调用以还原调用方的已保存 Enter 计数。When the caller reacquires the lock, the system calls Enter as many times as necessary to restore the saved Enter count for the caller. 调用 Wait 只释放指定对象的锁; 如果调用方是其他对象的锁的所有者,则不释放这些锁。Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

请注意,同步对象包含多个引用,包括对当前持有锁的线程的引用、对就绪队列的引用、包含准备好获取锁的线程的引用以及对等待队列的引用(其中包含等待对象状态更改通知的线程)。Note that a synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

Pulse PulseAll Wait 必须从同步的代码块中调用、和方法。The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

方法的备注 Pulse 说明 Pulse 了在没有线程等待时调用时所发生的情况。The remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.

另请参阅

适用于

产品 已引入

Wait(Object, Int32)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock. 如果已用指定的超时时间间隔,则线程进入就绪队列。If the specified time-out interval elapses, the thread enters the ready queue.

public:
 static bool Wait(System::Object ^ obj, int millisecondsTimeout);
public static bool Wait (object obj, int millisecondsTimeout);
static member Wait : obj * int -> bool
Public Shared Function Wait (obj As Object, millisecondsTimeout As Integer) As Boolean

参数

obj
Object

要在其上等待的对象。The object on which to wait.

millisecondsTimeout
Int32

线程进入就绪队列之前等待的毫秒数。The number of milliseconds to wait before the thread enters the ready queue.

返回

Boolean

如果在指定的时间过期之前重新获取该锁,则为 true;如果在指定的时间过期之后重新获取该锁,则为 falsetrue if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. 此方法只有在重新获取该锁后才会返回。The method does not return until the lock is reacquired.

例外

obj 参数为 nullThe obj parameter is null.

调用线程不拥有指定对象的锁。The calling thread does not own the lock for the specified object.

调用 Wait 的线程稍后从等待状态中断。The thread that invokes Wait is later interrupted from the waiting state. 另一线程调用此线程的 Interrupt() 方法时将发生这种情况。This happens when another thread calls this thread's Interrupt() method.

millisecondsTimeout 参数值为负且不等于 InfiniteThe value of the millisecondsTimeout parameter is negative, and is not equal to Infinite.

注解

此方法在重新获取该域参数的排他锁之前不会返回 objThis method does not return until it reacquires an exclusive lock on the obj parameter.

当前拥有指定对象的锁的线程调用此方法以释放该对象,以便其他线程可以访问它。The thread that currently owns the lock on the specified object invokes this method in order to release the object so that another thread can access it. 等待重新获取该锁时,调用方被阻止。The caller is blocked while waiting to reacquire the lock. 当调用方需要等待作为其他线程操作的结果而发生的状态更改时,将调用此方法。This method is called when the caller needs to wait for a state change that will occur as a result of another thread's operations.

超时可确保当另一个线程释放该锁,而不首先调用或方法时,当前线程不会无限期地阻止 Pulse PulseAllThe time-out ensures that the current thread does not block indefinitely if another thread releases the lock without first calling the Pulse or PulseAll method. 它还会将线程移动到就绪队列,绕过等待队列中的其他线程,以便它可以更快地重新获取锁定。It also moves the thread to the ready queue, bypassing other threads ahead of it in the wait queue, so that it can reacquire the lock sooner. 线程可以测试方法的返回值 Wait ,以确定它是否在超时前重新获取该锁。线程可以计算导致其进入等待状态的条件,并在必要时 Wait 再次调用方法。The thread can test the return value of the Wait method to determine whether it reacquired the lock prior to the time-out. The thread can evaluate the conditions that caused it to enter the wait, and if necessary call the Wait method again.

当线程调用时 Wait ,它会释放对象的锁,并进入该对象的等待队列。When a thread calls Wait, it releases the lock on the object and enters the object's waiting queue. 如果有一个) 获取了该锁并且独占使用了该对象,则该对象的 "就绪" 队列中的下一个线程 (。The next thread in the object's ready queue (if there is one) acquires the lock and has exclusive use of the object. 调用的线程将 Wait 保留在等待队列中,直到持有锁的线程调用 PulseAll ,或者它是队列中的下一线程以及持有锁的线程调用 PulseThe thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes PulseAll, or it is the next in the queue and a thread that holds the lock invokes Pulse. 但是,如果在 millisecondsTimeout 另一个线程调用此对象的 PulsePulseAll 方法之前已过,则原始线程将移到就绪队列,以便重新获取锁定。However, if millisecondsTimeout elapses before another thread invokes this object's Pulse or PulseAll method, the original thread is moved to the ready queue in order to regain the lock.

备注

如果 InfinitemillisecondsTimeout 参数指定,则此方法会无限期地阻止,除非锁的持有方调用 PulsePulseAllIf Infinite is specified for the millisecondsTimeout parameter, this method blocks indefinitely unless the holder of the lock calls Pulse or PulseAll. 如果 millisecondsTimeout 等于0,则调用的线程将 Wait 释放该锁,然后立即进入就绪队列以便重新获取锁。If millisecondsTimeout equals 0, the thread that calls Wait releases the lock and then immediately enters the ready queue in order to regain the lock.

Wait无论 Enter 为指定的对象调用了多少次,调用方都将执行一次。The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. 从概念上讲,此 Wait 方法存储调用方 Enter 在对象上调用的次数,并按 Exit 需多次调用以完全释放锁定的对象。Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit as many times as necessary to fully release the locked object. 然后,调用方在等待重新获取对象时阻止。The caller then blocks while waiting to reacquire the object. 当调用方重新获取该域锁定时,系统会根据 Enter 需要多次调用以还原调用方的已保存 Enter 计数。When the caller reacquires the lock, the system calls Enter as many times as necessary to restore the saved Enter count for the caller. 调用 Wait 只释放指定对象的锁; 如果调用方是其他对象的锁的所有者,则不释放这些锁。Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

备注

同步的对象包含多个引用,包括对当前持有锁的线程的引用、对就绪队列的引用、包含准备好获取锁的线程的引用以及对等待队列的引用(其中包含等待对象状态更改通知的线程)。A synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

Pulse PulseAll Wait 必须从同步的代码块中调用、和方法。The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

方法的备注 Pulse 说明 Pulse 了在没有线程等待时调用时所发生的情况。The remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.

另请参阅

适用于

产品 已引入

Wait(Object, TimeSpan)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock. 如果已用指定的超时时间间隔,则线程进入就绪队列。If the specified time-out interval elapses, the thread enters the ready queue.

public:
 static bool Wait(System::Object ^ obj, TimeSpan timeout);
public static bool Wait (object obj, TimeSpan timeout);
static member Wait : obj * TimeSpan -> bool
Public Shared Function Wait (obj As Object, timeout As TimeSpan) As Boolean

参数

obj
Object

要在其上等待的对象。The object on which to wait.

timeout
TimeSpan

TimeSpan,表示线程进入就绪队列之前等待的时间量。A TimeSpan representing the amount of time to wait before the thread enters the ready queue.

返回

Boolean

如果在指定的时间过期之前重新获取该锁,则为 true;如果在指定的时间过期之后重新获取该锁,则为 falsetrue if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. 此方法只有在重新获取该锁后才会返回。The method does not return until the lock is reacquired.

例外

obj 参数为 nullThe obj parameter is null.

调用线程不拥有指定对象的锁。The calling thread does not own the lock for the specified object.

调用 Wait 的线程稍后从等待状态中断。The thread that invokes Wait is later interrupted from the waiting state. 另一线程调用此线程的 Interrupt() 方法时将发生这种情况。This happens when another thread calls this thread's Interrupt() method.

timeout 参数的值(以毫秒计)为负数且不表示 Infinite(-1 毫秒),或者大于 MaxValueThe value of the timeout parameter in milliseconds is negative and does not represent Infinite (-1 millisecond), or is greater than MaxValue.

注解

此方法在重新获取该域参数的排他锁之前不会返回 objThis method does not return until it reacquires an exclusive lock on the obj parameter.

当前拥有指定对象的锁的线程调用此方法以释放该对象,以便其他线程可以访问它。The thread that currently owns the lock on the specified object invokes this method in order to release the object so that another thread can access it. 等待重新获取该锁时,调用方被阻止。The caller is blocked while waiting to reacquire the lock. 当调用方需要等待作为其他线程操作的结果而发生的状态更改时,将调用此方法。This method is called when the caller needs to wait for a state change that will occur as a result of another thread's operations.

超时可确保当另一个线程释放该锁,而不首先调用或方法时,当前线程不会无限期地阻止 Pulse PulseAllThe time-out ensures that the current thread does not block indefinitely if another thread releases the lock without first calling the Pulse or PulseAll method. 它还会将线程移动到就绪队列,绕过等待队列中的其他线程,以便它可以更快地重新获取锁定。It also moves the thread to the ready queue, bypassing other threads ahead of it in the wait queue, so that it can reacquire the lock sooner. 线程可以测试方法的返回值 Wait ,以确定它是否在超时前重新获取该锁。线程可以计算导致其进入等待状态的条件,并在必要时 Wait 再次调用方法。The thread can test the return value of the Wait method to determine whether it reacquired the lock prior to the time-out. The thread can evaluate the conditions that caused it to enter the wait, and if necessary call the Wait method again.

当线程调用时 Wait ,它会释放对象的锁,并进入该对象的等待队列。When a thread calls Wait, it releases the lock on the object and enters the object's waiting queue. 如果有一个) 获取了该锁并且独占使用了该对象,则该对象的 "就绪" 队列中的下一个线程 (。The next thread in the object's ready queue (if there is one) acquires the lock and has exclusive use of the object. 调用的线程将 Wait 保留在等待队列中,直到持有锁的线程调用 PulseAll ,或者它是队列中的下一线程以及持有锁的线程调用 PulseThe thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes PulseAll, or it is the next in the queue and a thread that holds the lock invokes Pulse. 但是,如果在 timeout 另一个线程调用此对象的 PulsePulseAll 方法之前已过,则原始线程将移到就绪队列,以便重新获取锁定。However, if timeout elapses before another thread invokes this object's Pulse or PulseAll method, the original thread is moved to the ready queue in order to regain the lock.

备注

如果为 TimeSpan 参数指定了表示-1 毫秒的 timeout ,则此方法会无限期阻止,除非锁的持有方调用 PulsePulseAllIf a TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely unless the holder of the lock calls Pulse or PulseAll. 如果 timeout 为0毫秒,则调用的线程将 Wait 释放该锁,然后立即进入就绪队列以便重新获取锁。If timeout is 0 milliseconds, the thread that calls Wait releases the lock and then immediately enters the ready queue in order to regain the lock.

Wait无论 Enter 为指定的对象调用了多少次,调用方都将执行一次。The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. 从概念上讲,此 Wait 方法存储调用方 Enter 在对象上调用的次数,并按 Exit 需多次调用以完全释放锁定的对象。Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit as many times as necessary to fully release the locked object. 然后,调用方在等待重新获取对象时阻止。The caller then blocks while waiting to reacquire the object. 当调用方重新获取该域锁定时,系统会根据 Enter 需要多次调用以还原调用方的已保存 Enter 计数。When the caller reacquires the lock, the system calls Enter as many times as necessary to restore the saved Enter count for the caller. 调用 Wait 只释放指定对象的锁; 如果调用方是其他对象的锁的所有者,则不释放这些锁。Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

备注

同步的对象包含多个引用,包括对当前持有锁的线程的引用、对就绪队列的引用、包含准备好获取锁的线程的引用以及对等待队列的引用(其中包含等待对象状态更改通知的线程)。A synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

Pulse PulseAll Wait 必须从同步的代码块中调用、和方法。The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

方法的备注 Pulse 说明 Pulse 了在没有线程等待时调用时所发生的情况。The remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.

另请参阅

适用于

产品 已引入

Wait(Object, TimeSpan, Boolean)

释放对象上的锁并阻止当前线程,直到它重新获取该锁。Releases the lock on an object and blocks the current thread until it reacquires the lock. 如果已用指定的超时时间间隔,则线程进入就绪队列。If the specified time-out interval elapses, the thread enters the ready queue. 可以在等待之前退出同步上下文的同步域,随后重新获取该域。Optionally exits the synchronization domain for the synchronized context before the wait and reacquires the domain afterward.

public:
 static bool Wait(System::Object ^ obj, TimeSpan timeout, bool exitContext);
public static bool Wait (object obj, TimeSpan timeout, bool exitContext);
static member Wait : obj * TimeSpan * bool -> bool
Public Shared Function Wait (obj As Object, timeout As TimeSpan, exitContext As Boolean) As Boolean

参数

obj
Object

要在其上等待的对象。The object on which to wait.

timeout
TimeSpan

TimeSpan,表示线程进入就绪队列之前等待的时间量。A TimeSpan representing the amount of time to wait before the thread enters the ready queue.

exitContext
Boolean

如果在等待前退出并重新获取上下文的同步域(如果在同步上下文中),则为 true;否则为 falsetrue to exit and reacquire the synchronization domain for the context (if in a synchronized context) before the wait; otherwise, false.

返回

Boolean

如果在指定的时间过期之前重新获取该锁,则为 true;如果在指定的时间过期之后重新获取该锁,则为 falsetrue if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. 此方法只有在重新获取该锁后才会返回。The method does not return until the lock is reacquired.

例外

obj 参数为 nullThe obj parameter is null.

Wait 不是从同步的代码块中调用的。Wait is not invoked from within a synchronized block of code.

调用 Wait 的线程稍后会从等待状态中中断。The thread that invokes Wait is later interrupted from the waiting state. 另一线程调用此线程的 Interrupt() 方法时将发生这种情况。This happens when another thread calls this thread's Interrupt() method.

timeout 参数为负数且不表示 Infinite(-1 毫秒),或者大于 MaxValueThe timeout parameter is negative and does not represent Infinite (-1 millisecond), or is greater than MaxValue.

注解

此方法在重新获取该域参数的排他锁之前不会返回 objThis method does not return until it reacquires an exclusive lock on the obj parameter.

当前拥有指定对象的锁的线程调用此方法以释放该对象,以便其他线程可以访问它。The thread that currently owns the lock on the specified object invokes this method in order to release the object so that another thread can access it. 等待重新获取该锁时,调用方被阻止。The caller is blocked while waiting to reacquire the lock. 当调用方需要等待作为其他线程操作的结果而发生的状态更改时,将调用此方法。This method is called when the caller needs to wait for a state change that will occur as a result of another thread's operations.

超时可确保当另一个线程释放该锁,而不首先调用或方法时,当前线程不会无限期地阻止 Pulse PulseAllThe time-out ensures that the current thread does not block indefinitely if another thread releases the lock without first calling the Pulse or PulseAll method. 它还会将线程移动到就绪队列,绕过等待队列中的其他线程,以便它可以更快地重新获取锁定。It also moves the thread to the ready queue, bypassing other threads ahead of it in the wait queue, so that it can reacquire the lock sooner. 线程可以测试方法的返回值 Wait ,以确定它是否在超时前重新获取该锁。线程可以计算导致其进入等待状态的条件,并在必要时 Wait 再次调用方法。The thread can test the return value of the Wait method to determine whether it reacquired the lock prior to the time-out. The thread can evaluate the conditions that caused it to enter the wait, and if necessary call the Wait method again.

当线程调用时 Wait ,它会释放该锁并进入等待队列。When a thread calls Wait, it releases the lock and enters the waiting queue. 此时,准备就绪的队列中的下一个线程 (如果允许一个) 控制该锁。At this point, the next thread in the ready queue (if there is one) is allowed to take control of the lock. 调用的线程将 Wait 保留在等待队列中,直到持有锁的线程调用 PulseAll ,或者它是队列中的下一线程以及持有锁的线程调用 PulseThe thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes PulseAll, or it is the next in the queue and a thread that holds the lock invokes Pulse. 但是,如果 timeout 在另一个线程调用此对象的或方法之前经过了毫秒 Pulse PulseAll ,则原始线程将移到就绪队列,以便重新获得锁定。However, if timeout milliseconds elapse before another thread invokes this object's Pulse or PulseAll method, the original thread is moved to the ready queue in order to regain the lock.

备注

如果为 TimeSpan 参数指定了表示-1 毫秒的 timeout ,则此方法会无限期阻止,除非锁的持有方调用 PulsePulseAllIf a TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely unless the holder of the lock calls Pulse or PulseAll. 如果 timeout 为0毫秒,则调用的线程将 Wait 释放该锁,然后立即进入就绪队列以便重新获取锁。If timeout is 0 milliseconds, the thread that calls Wait releases the lock and then immediately enters the ready queue in order to regain the lock.

Wait无论 Enter 为指定的对象调用了多少次,调用方都将执行一次。The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. 从概念上讲,此 Wait 方法存储调用方 Enter 在对象上调用的次数,并按 Exit 需多次调用以完全释放锁定的对象。Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit as many times as necessary to fully release the locked object. 然后,调用方在等待重新获取对象时阻止。The caller then blocks while waiting to reacquire the object. 当调用方重新获取该域锁定时,系统会根据 Enter 需要多次调用以还原调用方的已保存 Enter 计数。When the caller reacquires the lock, the system calls Enter as many times as necessary to restore the saved Enter count for the caller. 调用 Wait 只释放指定对象的锁; 如果调用方是其他对象的锁的所有者,则不释放这些锁。Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

备注

同步的对象包含多个引用,包括对当前持有锁的线程的引用、对就绪队列的引用、包含准备好获取锁的线程的引用以及对等待队列的引用(其中包含等待对象状态更改通知的线程)。A synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

Pulse PulseAll Wait 必须从同步的代码块中调用、和方法。The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

方法的备注 Pulse 说明 Pulse 了在没有线程等待时调用时所发生的情况。The remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.

有关退出上下文的说明Notes on Exiting the Context

exitContext除非 Wait 从非默认托管上下文内调用方法,否则参数不起作用。TheexitContext parameter has no effect unless the Wait method is called from inside a nondefault managed context. 如果你的线程在对从派生的类的实例的调用中,则可能会发生这种情况 ContextBoundObjectThis can happen if your thread is inside a call to an instance of a class derived from ContextBoundObject. 即使当前正在对不是从派生的类 ContextBoundObject (如)执行方法,如果在 String ContextBoundObject 当前应用程序域中的堆栈上,也可以在非默认上下文中执行。Even if you are currently executing a method on a class that is not derived from ContextBoundObject, like String, you can be in a nondefault context if a ContextBoundObject is on your stack in the current application domain.

当你的代码在非默认上下文中执行时,为指定将 true exitContext 导致线程退出非默认的托管上下文 (也就是说,在执行方法前转换为默认上下文) WaitWhen your code is executing in a nondefault context, specifying true for exitContext causes the thread to exit the nondefault managed context (that is, to transition to the default context) before executing the Wait method. 此方法在调用方法完成后返回到原始的非默认上下文 WaitIt returns to the original nondefault context after the call to the Wait method completes.

当上下文绑定类应用了属性时,这会很有用 SynchronizationAttributeThis can be useful when the context-bound class has the SynchronizationAttribute attribute applied. 在这种情况下,对类成员的所有调用都将自动同步,同步域是类的整个代码正文。In that case, all calls to members of the class are automatically synchronized, and the synchronization domain is the entire body of code for the class. 如果成员的调用堆栈中的代码调用 Wait 方法并 true 为指定 exitContext ,则线程将退出同步域,从而允许在对对象的任何成员的调用上被阻止的线程继续。If code in the call stack of a member calls the Wait method and specifies true for exitContext, the thread exits the synchronization domain, allowing a thread that is blocked on a call to any member of the object to proceed. Wait 方法返回时,进行调用的线程必须等待重新进入同步域。When the Wait method returns, the thread that made the call must wait to reenter the synchronization domain.

另请参阅

适用于

产品 已引入