Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo Monitor.Wait (Object, TimeSpan, Boolean)

 

Rilascia il blocco su un oggetto e interrompe il thread corrente finché riacquisisce il blocco. Allo scadere dell'intervallo di timeout specificato, il thread viene inserito nella coda di thread pronti. Esce eventualmente dal dominio di sincronizzazione per il contesto di sincronizzazione prima dell'attesa e riacquisisce il dominio in un secondo momento.

Spazio dei nomi:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public static bool Wait(
	object obj,
	TimeSpan timeout,
	bool exitContext
)

Parametri

obj
Type: System.Object

Oggetto per il quale attendere.

timeout
Type: System.TimeSpan

Oggetto TimeSpan che rappresenta il tempo di attesa prima che il thread venga inserito nella coda di thread pronti.

exitContext
Type: System.Boolean

true per uscire e riacquisire il dominio di sincronizzazione per il contesto, se si trova in un contesto sincronizzato, prima dell'attesa; in caso contrario, false.

Valore restituito

Type: System.Boolean

true se il blocco è stato riacquisito prima che sia trascorso il tempo specificato; false se il blocco è stato riacquisito dopo che è trascorso il tempo specificato. Il metodo non restituisce alcun valore finché il blocco non viene riacquisito.

Exception Condition
ArgumentNullException

Il valore del parametro obj è null.

SynchronizationLockException

Wait non viene richiamato dall'interno di un blocco di codice sincronizzato.

ThreadInterruptedException

Il thread da cui è stato richiamato Wait viene interrotto più tardi dallo stato di attesa. L'interruzione si verifica quando il metodo Interrupt di questo thread viene chiamato da un altro thread.

ArgumentOutOfRangeException

Il parametro timeout è negativo e non rappresenta Infinite (– 1 millisecondo) oppure è maggiore di MaxValue.

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.

The time-out ensures that the current thread does not block indefinitely if another thread releases the lock without first calling the M:System.Threading.Monitor.Pulse(System.Object) or M:System.Threading.Monitor.PulseAll(System.Object) 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. The thread can test the return value of the M:System.Threading.Monitor.Wait(System.Object,System.TimeSpan,System.Boolean) 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 M:System.Threading.Monitor.Wait(System.Object,System.TimeSpan,System.Boolean) method again.

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. The thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes M:System.Threading.Monitor.PulseAll(System.Object), or it is the next in the queue and a thread that holds the lock invokes M:System.Threading.Monitor.Pulse(System.Object). However, if timeout milliseconds elapse before another thread invokes this object's M:System.Threading.Monitor.Pulse(System.Object) or M:System.Threading.Monitor.PulseAll(System.Object) method, the original thread is moved to the ready queue in order to regain the lock.

System_CAPS_noteNota

If a T:System.TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely unless the holder of the lock calls M:System.Threading.Monitor.Pulse(System.Object) or M:System.Threading.Monitor.PulseAll(System.Object). 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.

The caller executes Wait once, regardless of the number of times M:System.Threading.Monitor.Enter(System.Object) has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked M:System.Threading.Monitor.Enter(System.Object) on the object and invokes M:System.Threading.Monitor.Exit(System.Object) as many times as necessary to fully release the locked object. The caller then blocks while waiting to reacquire the object. When the caller reacquires the lock, the system calls M:System.Threading.Monitor.Enter(System.Object) as many times as necessary to restore the saved M:System.Threading.Monitor.Enter(System.Object) count for the caller. 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.

System_CAPS_noteNota

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.

The M:System.Threading.Monitor.Pulse(System.Object), M:System.Threading.Monitor.PulseAll(System.Object), and Wait methods must be invoked from within a synchronized block of code.

The remarks for the M:System.Threading.Monitor.Pulse(System.Object) method explain what happens if M:System.Threading.Monitor.Pulse(System.Object) is called when no threads are waiting.

TheexitContext parameter has no effect unless the M:System.Threading.Monitor.Wait(System.Object,System.TimeSpan,System.Boolean) method is called from inside a nondefault managed context. This can happen if your thread is inside a call to an instance of a class derived from T:System.ContextBoundObject. Even if you are currently executing a method on a class that is not derived from T:System.ContextBoundObject, like T:System.String, you can be in a nondefault context if a T:System.ContextBoundObject is on your stack in the current application domain.

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 M:System.Threading.Monitor.Wait(System.Object,System.TimeSpan,System.Boolean) method. It returns to the original nondefault context after the call to the M:System.Threading.Monitor.Wait(System.Object,System.TimeSpan,System.Boolean) method completes.

This can be useful when the context-bound class has the T:System.Runtime.Remoting.Contexts.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. If code in the call stack of a member calls the M:System.Threading.Monitor.Wait(System.Object,System.TimeSpan,System.Boolean) 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. When the M:System.Threading.Monitor.Wait(System.Object,System.TimeSpan,System.Boolean) method returns, the thread that made the call must wait to reenter the synchronization domain.

.NET Framework
Disponibile da 1.1
Torna all'inizio
Mostra: