ReaderWriterLock.ReleaseWriterLock 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
減少寫入鎖的鎖數。
public:
void ReleaseWriterLock();
public void ReleaseWriterLock();
member this.ReleaseWriterLock : unit -> unit
Public Sub ReleaseWriterLock ()
例外狀況
這個討論串沒有寫者鎖定。
範例
以下程式碼範例展示了如何取得並釋放寫入鎖定,以及如何處理請求逾時拋出的例外。
此程式碼是該類別更大 ReaderWriterLock 範例的一部分。
// 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
備註
ReleaseWriterLock 減少寫入鎖定數量。 當計數歸零時,寫入鎖定會被解除。
備註
如果執行緒有讀取鎖或沒有鎖定,呼叫 ReleaseWriterLock 會拋出一個 ApplicationException。
適用於
另請參閱
- 管理的執行緒
- ReaderWriterLock