ReaderWriterLock.AcquireReaderLock 方法     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取读线程锁。
重载
| AcquireReaderLock(Int32) | 使用一个 Int32 超时值获取读线程锁。 | 
| AcquireReaderLock(TimeSpan) | 使用一个 TimeSpan 超时值获取读线程锁。 | 
AcquireReaderLock(Int32)
- Source:
- ReaderWriterLock.cs
- Source:
- ReaderWriterLock.cs
- Source:
- ReaderWriterLock.cs
- Source:
- ReaderWriterLock.cs
使用一个 Int32 超时值获取读线程锁。
public:
 void AcquireReaderLock(int millisecondsTimeout);[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void AcquireReaderLock(int millisecondsTimeout);public void AcquireReaderLock(int millisecondsTimeout);[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.AcquireReaderLock : int -> unitmember this.AcquireReaderLock : int -> unitPublic Sub AcquireReaderLock (millisecondsTimeout As Integer)参数
- millisecondsTimeout
- Int32
以毫秒为单位的超时。
- 属性
例外
              millisecondsTimeout 在授予锁定请求前过期。
示例
下面的代码示例演示如何获取和释放读取器锁,以及如何处理请求超时时引发的异常。
此代码是为 类提供的更大示例的 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 a reader lock, and handle time-outs.
static void ReadFromResource(int timeOut)
{
   try {
      rwl.AcquireReaderLock(timeOut);
      try {
         // It is safe for this thread to read from the shared resource.
         Display("reads resource value " + resource);
         Interlocked.Increment(ref reads);
      }
      finally {
         // Ensure that the lock is released.
         rwl.ReleaseReaderLock();
      }
   }
   catch (ApplicationException) {
      // The reader lock request timed out.
      Interlocked.Increment(ref readerTimeouts);
   }
}
' Request and release a reader lock, and handle time-outs.
Sub ReadFromResource(timeOut As Integer)
   Try
      rwl.AcquireReaderLock(timeOut)
      Try
         ' It's safe for this thread to read from the shared resource.
         Display("reads resource value " & resource)
         Interlocked.Increment(reads)
      Finally
         ' Ensure that the lock is released.
         rwl.ReleaseReaderLock()
      End Try
   Catch ex As ApplicationException
      ' The reader lock request timed out.
      Interlocked.Increment(readerTimeouts)
   End Try
End Sub
}
End Module
注解
AcquireReaderLock 如果其他线程具有编写器锁,或者至少有一个线程正在等待编写器锁,则阻塞。
注意
如果当前线程已具有编写器锁,则不会获取读取器锁。 相反,编写器锁上的锁计数会递增。 这可以防止线程在其自己的编写器锁上阻塞。 结果与调用 AcquireWriterLock完全相同,释放编写器锁时需要对 进行额外的调用 ReleaseWriterLock 。
              AcquireReaderLock 支持递归读取器锁请求。 也就是说,线程可以多次调用 AcquireReaderLock,这每次都会递增锁计数。 每次调用 ReleaseReaderLock 时,必须调用 AcquireReaderLock一次。 或者,可以调用 ReleaseLock 将锁计数立即减少到零。
递归锁请求始终立即授予,而不会将请求线程置于读取器队列中。 请谨慎使用递归锁,以避免长时间阻止编写器锁请求。
有关有效的超时值,请参阅 ReaderWriterLock。
另请参阅
适用于
AcquireReaderLock(TimeSpan)
- Source:
- ReaderWriterLock.cs
- Source:
- ReaderWriterLock.cs
- Source:
- ReaderWriterLock.cs
- Source:
- ReaderWriterLock.cs
使用一个 TimeSpan 超时值获取读线程锁。
public:
 void AcquireReaderLock(TimeSpan timeout);[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void AcquireReaderLock(TimeSpan timeout);public void AcquireReaderLock(TimeSpan timeout);[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.AcquireReaderLock : TimeSpan -> unitmember this.AcquireReaderLock : TimeSpan -> unitPublic Sub AcquireReaderLock (timeout As TimeSpan)参数
- timeout
- TimeSpan
一个 TimeSpan,用于指定超时时间。
- 属性
例外
              timeout 在授予锁定请求前过期。
              timeout 可指定 -1 毫秒以外的任何负值。
注解
AcquireReaderLock 如果其他线程具有编写器锁,或者至少有一个线程正在等待编写器锁,则阻塞。
注意
如果当前线程已具有编写器锁,则不会获取读取器锁。 相反,编写器锁上的锁计数会递增。 这可以防止线程在其自己的编写器锁上阻塞。 结果与调用 AcquireWriterLock完全相同,释放编写器锁时需要对 进行额外的调用 ReleaseWriterLock 。
              AcquireReaderLock 支持递归读取器锁请求。 也就是说,线程可以多次调用 AcquireReaderLock,这每次都会递增锁计数。 每次调用 ReleaseReaderLock 时,必须调用 AcquireReaderLock一次。 或者,可以调用 ReleaseLock 将锁计数立即减少到零。
递归锁请求始终立即授予,而不会将请求线程置于读取器队列中。 请谨慎使用递归锁,以避免长时间阻止编写器锁请求。
有关有效的超时值,请参阅 ReaderWriterLock。