ReaderWriterLock.ReleaseWriterLock Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Decrementa il conteggio dei blocchi sul blocco writer.
public:
void ReleaseWriterLock();
public void ReleaseWriterLock();
member this.ReleaseWriterLock : unit -> unit
Public Sub ReleaseWriterLock ()
Eccezioni
Il thread non dispone del blocco writer.
Esempio
Nell'esempio di codice seguente viene illustrato come acquisire e rilasciare un blocco writer e come gestire l'eccezione generata quando si verifica il timeout di una richiesta.
Questo codice fa parte di un esempio più ampio fornito per la ReaderWriterLock classe .
// The complete code is located in the ReaderWriterLock class topic.
using System;
using System.Threading;
public class Example
{
static ReaderWriterLock rwl = new ReaderWriterLock();
// Define the shared resource protected by the ReaderWriterLock.
static int resource = 0;
' The complete code is located in the ReaderWriterLock class topic.
Imports System.Threading
Public Module Example
Private rwl As New ReaderWriterLock()
' Define the shared resource protected by the ReaderWriterLock.
Private resource As Integer = 0
// Request and release the writer lock, and handle time-outs.
static void WriteToResource(Random rnd, int timeOut)
{
try {
rwl.AcquireWriterLock(timeOut);
try {
// It's safe for this thread to access from the shared resource.
resource = rnd.Next(500);
Display("writes resource value " + resource);
Interlocked.Increment(ref writes);
}
finally {
// Ensure that the lock is released.
rwl.ReleaseWriterLock();
}
}
catch (ApplicationException) {
// The writer lock request timed out.
Interlocked.Increment(ref writerTimeouts);
}
}
' Request and release the writer lock, and handle time-outs.
Sub WriteToResource(rnd As Random, timeOut As Integer)
Try
rwl.AcquireWriterLock(timeOut)
Try
' It's safe for this thread to read or write from the shared resource.
resource = rnd.Next(500)
Display("writes resource value " & resource)
Interlocked.Increment(writes)
Finally
' Ensure that the lock is released.
rwl.ReleaseWriterLock()
End Try
Catch ex As ApplicationException
' The writer lock request timed out.
Interlocked.Increment(writerTimeouts)
End Try
End Sub
}
End Module
Commenti
ReleaseWriterLock decrementa il conteggio dei blocchi del writer. Quando il conteggio raggiunge zero, viene rilasciato il blocco writer.
Note
Se un thread ha un blocco lettore o nessun blocco, la chiamata ReleaseWriterLock genera un'eccezione ApplicationException.