本文整理汇总了C#中System.Threading.ReaderWriterLockSlim.TryEnterWriteLock方法的典型用法代码示例。如果您正苦于以下问题:C# ReaderWriterLockSlim.TryEnterWriteLock方法的具体用法?C# ReaderWriterLockSlim.TryEnterWriteLock怎么用?C# ReaderWriterLockSlim.TryEnterWriteLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.ReaderWriterLockSlim
的用法示例。
在下文中一共展示了ReaderWriterLockSlim.TryEnterWriteLock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteLock
public WriteLock(ReaderWriterLockSlim rwl, int millisecondsTimeout)
: base(rwl)
{
if (millisecondsTimeout == Timeout.Infinite)
{
while (!_lockAcquired)
_lockAcquired = rwl.TryEnterWriteLock(1);
}
else
{
_lockAcquired = rwl.TryEnterWriteLock(millisecondsTimeout);
}
}
示例2: WriteLock
public WriteLock(ReaderWriterLockSlim @lock, int millisecondsTimeout)
{
_lock = @lock;
_isDisposed = 0;
if (!_lock.TryEnterWriteLock(millisecondsTimeout))
throw new TimeoutException();
}
示例3: GetWriteLock
/// <summary>
/// The get write lock.
/// </summary>
/// <param name="locks"> The locks. </param>
public static void GetWriteLock(ReaderWriterLockSlim locks)
{
bool lockAcquired = false;
while (!lockAcquired)
{
lockAcquired = locks.TryEnterWriteLock(1);
}
}
示例4: SlimWriteLockHolder
public SlimWriteLockHolder(ReaderWriterLockSlim locker, bool waitForLock)
{
this.locker = locker;
if(waitForLock)
{
locker.EnterWriteLock();
lockAcquired = true;
return;
}
lockAcquired = locker.TryEnterWriteLock(0);
}
示例5: CantAqureLock
public void CantAqureLock()
{
var l = new ReaderWriterLockSlim();
var e = new AutoResetEvent(false);
var t = new Thread(() =>
{
var b =l.TryEnterWriteLock(TimeSpan.FromSeconds(1));
Assert.IsTrue(b);
e.Set();
Thread.Sleep(TimeSpan.FromMinutes(10));
l.Dispose();
});
t.Start();
e.WaitOne();
var a = new DisposableReaderWriterLockSlim(l, TimeSpan.FromSeconds(3), SlimLockMode.Write);
}
示例6: Dispose
public static void Dispose()
{
ReaderWriterLockSlim rwls;
rwls = new ReaderWriterLockSlim();
rwls.Dispose();
Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterReadLock(0));
Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterUpgradeableReadLock(0));
Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterWriteLock(0));
rwls.Dispose();
for (int i = 0; i < 3; i++)
{
rwls = new ReaderWriterLockSlim();
switch (i)
{
case 0: rwls.EnterReadLock(); break;
case 1: rwls.EnterUpgradeableReadLock(); break;
case 2: rwls.EnterWriteLock(); break;
}
Assert.Throws<SynchronizationLockException>(() => rwls.Dispose());
}
}
示例7: EnterWriteLock_After_ExitUpgradeableReadLock
public void EnterWriteLock_After_ExitUpgradeableReadLock ()
{
var v = new ReaderWriterLockSlim ();
v.EnterUpgradeableReadLock ();
Assert.IsTrue (v.TryEnterWriteLock (100));
v.ExitWriteLock ();
v.ExitUpgradeableReadLock ();
Assert.IsTrue (v.TryEnterWriteLock (100));
v.ExitWriteLock ();
}
示例8: TryEnterWriteLock_WhileReading
public void TryEnterWriteLock_WhileReading ()
{
var v = new ReaderWriterLockSlim ();
AutoResetEvent ev = new AutoResetEvent (false);
AutoResetEvent ev2 = new AutoResetEvent (false);
Thread t1 = new Thread (() => {
v.EnterReadLock ();
ev2.Set ();
ev.WaitOne ();
v.ExitReadLock ();
});
t1.Start ();
ev2.WaitOne ();
Assert.IsFalse (v.TryEnterWriteLock (100));
Assert.IsFalse (v.TryEnterReadLock (100));
ev.Set ();
Assert.IsTrue (v.TryEnterWriteLock (100));
}
示例9: TryEnterWriteLock_OutOfRange
public void TryEnterWriteLock_OutOfRange ()
{
var v = new ReaderWriterLockSlim ();
try {
v.TryEnterWriteLock (-2);
Assert.Fail ("1");
} catch (ArgumentOutOfRangeException) {
}
try {
v.TryEnterWriteLock (TimeSpan.MaxValue);
Assert.Fail ("2");
} catch (ArgumentOutOfRangeException) {
}
try {
v.TryEnterWriteLock (TimeSpan.MinValue);
Assert.Fail ("3");
} catch (ArgumentOutOfRangeException) {
}
}
示例10: EnterExit
public static void EnterExit()
{
using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim())
{
Assert.False(rwls.IsReadLockHeld);
rwls.EnterReadLock();
Assert.True(rwls.IsReadLockHeld);
rwls.ExitReadLock();
Assert.False(rwls.IsReadLockHeld);
Assert.False(rwls.IsUpgradeableReadLockHeld);
rwls.EnterUpgradeableReadLock();
Assert.True(rwls.IsUpgradeableReadLockHeld);
rwls.ExitUpgradeableReadLock();
Assert.False(rwls.IsUpgradeableReadLockHeld);
Assert.False(rwls.IsWriteLockHeld);
rwls.EnterWriteLock();
Assert.True(rwls.IsWriteLockHeld);
rwls.ExitWriteLock();
Assert.False(rwls.IsWriteLockHeld);
Assert.False(rwls.IsUpgradeableReadLockHeld);
rwls.EnterUpgradeableReadLock();
Assert.False(rwls.IsWriteLockHeld);
Assert.True(rwls.IsUpgradeableReadLockHeld);
rwls.EnterWriteLock();
Assert.True(rwls.IsWriteLockHeld);
rwls.ExitWriteLock();
Assert.False(rwls.IsWriteLockHeld);
Assert.True(rwls.IsUpgradeableReadLockHeld);
rwls.ExitUpgradeableReadLock();
Assert.False(rwls.IsUpgradeableReadLockHeld);
Assert.True(rwls.TryEnterReadLock(0));
rwls.ExitReadLock();
Assert.True(rwls.TryEnterReadLock(Timeout.InfiniteTimeSpan));
rwls.ExitReadLock();
Assert.True(rwls.TryEnterUpgradeableReadLock(0));
rwls.ExitUpgradeableReadLock();
Assert.True(rwls.TryEnterUpgradeableReadLock(Timeout.InfiniteTimeSpan));
rwls.ExitUpgradeableReadLock();
Assert.True(rwls.TryEnterWriteLock(0));
rwls.ExitWriteLock();
Assert.True(rwls.TryEnterWriteLock(Timeout.InfiniteTimeSpan));
rwls.ExitWriteLock();
}
}
示例11: WriterToWriterChain
public static void WriterToWriterChain()
{
using (AutoResetEvent are = new AutoResetEvent(false))
using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim())
{
rwls.EnterWriteLock();
Task t = Task.Factory.StartNew(() =>
{
Assert.False(rwls.TryEnterWriteLock(10));
Task.Run(() => are.Set()); // ideally this won't fire until we've called EnterWriteLock, but it's a benign race in that the test will succeed either way
rwls.EnterWriteLock();
rwls.ExitWriteLock();
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
are.WaitOne();
rwls.ExitWriteLock();
t.GetAwaiter().GetResult();
}
}
示例12: WritersAreMutuallyExclusiveFromWriters
public static void WritersAreMutuallyExclusiveFromWriters()
{
using (Barrier barrier = new Barrier(2))
using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim())
{
Task.WaitAll(
Task.Run(() =>
{
rwls.EnterWriteLock();
barrier.SignalAndWait();
Assert.True(rwls.IsWriteLockHeld);
barrier.SignalAndWait();
rwls.ExitWriteLock();
}),
Task.Run(() =>
{
barrier.SignalAndWait();
Assert.False(rwls.TryEnterWriteLock(0));
Assert.False(rwls.IsReadLockHeld);
barrier.SignalAndWait();
}));
}
}
示例13: InvalidTimeouts
public static void InvalidTimeouts()
{
using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim())
{
Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterReadLock(-2));
Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterUpgradeableReadLock(-3));
Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterWriteLock(-4));
Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterReadLock(TimeSpan.MaxValue));
Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterUpgradeableReadLock(TimeSpan.MinValue));
Assert.Throws<ArgumentOutOfRangeException>(() => rwls.TryEnterWriteLock(TimeSpan.FromMilliseconds(-2)));
}
}
示例14: QuickTryEnterWriteLock
/// <summary>
///
/// </summary>
/// <param name="readerWriterLockSlim"></param>
/// <returns></returns>
public static bool QuickTryEnterWriteLock( ReaderWriterLockSlim readerWriterLockSlim )
{
return readerWriterLockSlim.TryEnterWriteLock( s_TimeSpan );
}
示例15: WriteLock
/// <summary>
/// Acquires a write lock on a lock object. If the lock object already holds a write
/// lock, no new lock is acquired and no lock will be released.
/// </summary>
/// <param name="rwlock">Lock object to work with. If this is null, no lock will be acquired.</param>
public WriteLock(ReaderWriterLockSlim rwlock)
{
this.rwlock = rwlock;
if (rwlock == null)
wasLocked = true;
else
wasLocked = rwlock.IsWriteLockHeld;
if (!wasLocked && !rwlock.TryEnterWriteLock(WriteLockTimeout))
{
throw new InvalidOperationException("The writer lock could not be acquired.");
}
}