本文整理汇总了C#中System.Threading.ReaderWriterLock.DowngradeFromWriterLock方法的典型用法代码示例。如果您正苦于以下问题:C# ReaderWriterLock.DowngradeFromWriterLock方法的具体用法?C# ReaderWriterLock.DowngradeFromWriterLock怎么用?C# ReaderWriterLock.DowngradeFromWriterLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.ReaderWriterLock
的用法示例。
在下文中一共展示了ReaderWriterLock.DowngradeFromWriterLock方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DowngradeTest
public void DowngradeTest ()
{
LockCookie lc1, lc2, lc3, lc4;
rwlock = new ReaderWriterLock ();
rwlock.AcquireReaderLock (Timeout.Infinite);
lc1 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
rwlock.AcquireReaderLock (Timeout.Infinite);
lc2 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
rwlock.AcquireReaderLock (Timeout.Infinite);
lc3 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
rwlock.AcquireReaderLock (Timeout.Infinite);
lc4 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
rwlock.DowngradeFromWriterLock (ref lc2);
Assert.IsFalse (rwlock.IsReaderLockHeld, "A1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "A2");
rwlock.ReleaseReaderLock ();
Assert.IsFalse (rwlock.IsReaderLockHeld, "B1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "B2");
rwlock.DowngradeFromWriterLock (ref lc4);
Assert.IsFalse (rwlock.IsReaderLockHeld, "C1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "C2");
rwlock.ReleaseReaderLock ();
Assert.IsFalse (rwlock.IsReaderLockHeld, "D1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "D2");
rwlock.DowngradeFromWriterLock (ref lc3);
Assert.IsFalse (rwlock.IsReaderLockHeld, "E1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "E2");
rwlock.ReleaseReaderLock ();
Assert.IsFalse (rwlock.IsReaderLockHeld, "F1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "F2");
rwlock.DowngradeFromWriterLock (ref lc1);
Assert.IsTrue (rwlock.IsReaderLockHeld, "G1");
Assert.IsFalse (rwlock.IsWriterLockHeld, "G2");
rwlock.ReleaseReaderLock ();
Assert.IsFalse (rwlock.IsReaderLockHeld, "H1");
Assert.IsFalse (rwlock.IsWriterLockHeld, "H2");
}
示例2: UpgradeToWriterLock
public static IDisposable UpgradeToWriterLock(ReaderWriterLock rwl)
{
var lockCookie = rwl.UpgradeToWriterLock(Timeout.Infinite);
return new Disposer(() => rwl.DowngradeFromWriterLock(ref lockCookie));
}
示例3: TestUpgradeDowngradeLock
public void TestUpgradeDowngradeLock ()
{
rwlock = new ReaderWriterLock ();
rwlock.AcquireReaderLock (200);
rwlock.AcquireReaderLock (200);
LockCookie co = rwlock.UpgradeToWriterLock (200);
Assert.IsTrue (!rwlock.IsReaderLockHeld);
Assert.IsTrue (rwlock.IsWriterLockHeld);
RunThread (new ThreadStart (AcquireLock_writerFails));
rwlock.DowngradeFromWriterLock (ref co);
Assert.IsTrue (rwlock.IsReaderLockHeld);
Assert.IsTrue (!rwlock.IsWriterLockHeld);
RunThread (new ThreadStart (AcquireLock_readerWorks));
rwlock.ReleaseReaderLock ();
Assert.IsTrue (rwlock.IsReaderLockHeld);
rwlock.ReleaseReaderLock ();
Assert.IsTrue (!rwlock.IsReaderLockHeld);
}
示例4: ExecuteJob
/// <summary>
/// Executes a job on demand, rather than waiting for its regularly scheduled time.
/// </summary>
/// <param name="job">The job to be executed.</param>
public static void ExecuteJob(JobBase job)
{
ReaderWriterLock rwLock = new ReaderWriterLock();
try
{
rwLock.AcquireReaderLock(Timeout.Infinite);
if (job.Executing == false)
{
LockCookie lockCookie = rwLock.UpgradeToWriterLock(Timeout.Infinite);
try
{
if (job.Executing == false)
{
job.Executing = true;
QueueJob(job);
}
}
finally
{
rwLock.DowngradeFromWriterLock(ref lockCookie);
}
}
}
finally
{
rwLock.ReleaseReaderLock();
}
}