Monitor.Wait Monitor.Wait Monitor.Wait Monitor.Wait Method

定义

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

重载

Wait(Object, Int32, Boolean) Wait(Object, Int32, Boolean) Wait(Object, Int32, Boolean) 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) Wait(Object) Wait(Object) Wait(Object)

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

Wait(Object, Int32) Wait(Object, Int32) Wait(Object, Int32) 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) Wait(Object, TimeSpan) Wait(Object, TimeSpan) 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) Wait(Object, TimeSpan, Boolean) Wait(Object, TimeSpan, Boolean) 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) Wait(Object, Int32, Boolean) Wait(Object, Int32, Boolean) 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 Object Object Object

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

millisecondsTimeout
Int32 Int32 Int32 Int32

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

exitContext
Boolean Boolean Boolean Boolean

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

返回

如果在指定的时间过期之前重新获取该锁,则为 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.

异常

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.

注解

此方法不返回直到它重新上获取排他锁obj参数。This 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.

超时可确保,当前线程不会无限期阻塞如果另一个线程释放锁,但第一个调用不PulsePulseAll方法。The 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.

备注

如果Infinite为指定millisecondsTimeout参数,此方法会无限期阻止除非锁持有者调用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.

PulsePulseAll,和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会导致线程退出非默认托管的上下文 (即,转换到默认上下文) 执行前Wait方法。When 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. 在调用后返回到原始的非默认上下文Wait方法完成。It returns to the original nondefault context after the call to the Wait method completes.

在上下文绑定类时这很有用SynchronizationAttribute应用属性。This 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方法,并指定trueexitContext,在线程退出同步域,这样在调用对象的任何成员以继续操作被阻止的线程。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) Wait(Object) Wait(Object) 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 Object Object Object

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

返回

如果调用由于调用方重新获取了指定对象的锁而返回,则为 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.

异常

调用线程不拥有指定对象的锁。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保留在等待队列,直到它们接收来自信号PulsePulseAll、 发送的锁的所有者。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. 请注意,如果锁持有者不会调用该方法会阻止无限期地PulsePulseAllNote 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.

PulsePulseAll,和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) Wait(Object, Int32) Wait(Object, Int32) 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 Object Object Object

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

millisecondsTimeout
Int32 Int32 Int32 Int32

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

返回

如果在指定的时间过期之前重新获取该锁,则为 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.

异常

调用线程不拥有指定对象的锁。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.

注解

此方法不返回直到它重新上获取排他锁obj参数。This 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.

超时可确保,当前线程不会无限期阻塞如果另一个线程释放锁,但第一个调用不PulsePulseAll方法。The 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.

备注

如果Infinite为指定millisecondsTimeout参数,此方法会无限期阻止除非锁持有者调用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.

PulsePulseAll,和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) Wait(Object, TimeSpan) Wait(Object, TimeSpan) 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 Object Object Object

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

timeout
TimeSpan TimeSpan TimeSpan TimeSpan

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

返回

如果在指定的时间过期之前重新获取该锁,则为 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.

异常

调用线程不拥有指定对象的锁。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.

注解

此方法不返回直到它重新上获取排他锁obj参数。This 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.

超时可确保,当前线程不会无限期阻塞如果另一个线程释放锁,但第一个调用不PulsePulseAll方法。The 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.

PulsePulseAll,和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) Wait(Object, TimeSpan, Boolean) Wait(Object, TimeSpan, Boolean) 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 Object Object Object

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

timeout
TimeSpan TimeSpan TimeSpan TimeSpan

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

exitContext
Boolean Boolean Boolean Boolean

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

返回

如果在指定的时间过期之前重新获取该锁,则为 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.

异常

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.

注解

此方法不返回直到它重新上获取排他锁obj参数。This 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.

超时可确保,当前线程不会无限期阻塞如果另一个线程释放锁,但第一个调用不PulsePulseAll方法。The 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毫秒等待另一个线程调用此对象之前PulsePulseAll方法,在原始线程将移动到准备就绪的队列,以便重新获取该锁。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.

PulsePulseAll,和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会导致线程退出非默认托管的上下文 (即,转换到默认上下文) 执行前Wait方法。When 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. 在调用后返回到原始的非默认上下文Wait方法完成。It returns to the original nondefault context after the call to the Wait method completes.

在上下文绑定类时这很有用SynchronizationAttribute应用属性。This 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方法,并指定trueexitContext,在线程退出同步域,这样在调用对象的任何成员以继续操作被阻止的线程。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.

另请参阅

适用于