当前位置: 首页>>代码示例>>C#>>正文


C# ReaderWriterLockSlim.TryEnterReadLock方法代码示例

本文整理汇总了C#中System.Threading.ReaderWriterLockSlim.TryEnterReadLock方法的典型用法代码示例。如果您正苦于以下问题:C# ReaderWriterLockSlim.TryEnterReadLock方法的具体用法?C# ReaderWriterLockSlim.TryEnterReadLock怎么用?C# ReaderWriterLockSlim.TryEnterReadLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.ReaderWriterLockSlim的用法示例。


在下文中一共展示了ReaderWriterLockSlim.TryEnterReadLock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReadOnlyLock

 public ReadOnlyLock(ReaderWriterLockSlim rwl, int millisecondsTimeout)
     : base(rwl)
 {
     if (millisecondsTimeout == Timeout.Infinite)
     {
         while (!_lockAcquired)
             _lockAcquired = rwl.TryEnterReadLock(1);
     }
     else
     {
         _lockAcquired = rwl.TryEnterReadLock(millisecondsTimeout);
     }
 }
开发者ID:footprint4me,项目名称:objectpool,代码行数:13,代码来源:ReadOnlyLock.cs

示例2: ReadLock

		public ReadLock(ReaderWriterLockSlim @lock, int millisecondsTimeout)
		{
			_lock = @lock;
			_isDisposed = 0;

			if (!_lock.TryEnterReadLock(millisecondsTimeout))
				throw new TimeoutException();
		}
开发者ID:polewskm,项目名称:NCode.Composition.DisposableParts,代码行数:8,代码来源:ReadLock.cs

示例3: GetReadOnlyLock

 /// <summary>
 /// The get read only lock.
 /// </summary>
 /// <param name="locks"> The locks. </param>
 public static void GetReadOnlyLock(ReaderWriterLockSlim locks)
 {
     bool lockAcquired = false;
     while (!lockAcquired)
     {
         lockAcquired = locks.TryEnterReadLock(1);
     }
 }
开发者ID:angle163,项目名称:Practice,代码行数:12,代码来源:Locks.cs

示例4: SlimReadLockHolder

		public SlimReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock)
		{
			this.locker = locker;
			if(waitForLock)
			{
				locker.EnterReadLock();
				lockAcquired = true;
				return;
			}
			lockAcquired = locker.TryEnterReadLock(0);
		}
开发者ID:ArthurYiL,项目名称:JustMockLite,代码行数:11,代码来源:SlimReadLockHolder.cs

示例5: TryEnterReadLock_OutOfRange

		public void TryEnterReadLock_OutOfRange ()
		{
			var v = new ReaderWriterLockSlim ();
			try {
				v.TryEnterReadLock (-2);
				Assert.Fail ("1");
			} catch (ArgumentOutOfRangeException) {
			}

			try {
				v.TryEnterReadLock (TimeSpan.MaxValue);
				Assert.Fail ("2");
			} catch (ArgumentOutOfRangeException) {
			}

			try {
				v.TryEnterReadLock (TimeSpan.MinValue);
				Assert.Fail ("3");
			} catch (ArgumentOutOfRangeException) {
			}
		}
开发者ID:cthrax,项目名称:mono,代码行数:21,代码来源:ReaderWriterLockSlimTest.cs

示例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());
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:23,代码来源:ReaderWriterLockSlimTests.cs

示例7: RecursiveWritePlusReadLockTest

		public void RecursiveWritePlusReadLockTest ()
		{
			var v = new ReaderWriterLockSlim (LockRecursionPolicy.SupportsRecursion);

			Assert.IsTrue (v.TryEnterWriteLock (100), "#1");
			Assert.AreEqual (1, v.RecursiveWriteCount, "1b");
			Assert.AreEqual (0, v.RecursiveReadCount, "1c");

			Assert.IsTrue (v.TryEnterReadLock (100), "#2");
			Assert.AreEqual (1, v.RecursiveWriteCount, "2b");
			Assert.AreEqual (1, v.RecursiveReadCount, "2c");

			Assert.IsTrue (v.TryEnterReadLock (100), "#3");
			Assert.AreEqual (1, v.RecursiveWriteCount, "3b");
			Assert.AreEqual (2, v.RecursiveReadCount, "3c");

			v.ExitReadLock ();
			Assert.AreEqual (1, v.RecursiveWriteCount, "4b");
			Assert.AreEqual (1, v.RecursiveReadCount, "4c");
		}
开发者ID:cthrax,项目名称:mono,代码行数:20,代码来源:ReaderWriterLockSlimTest.cs

示例8: RecursiveReadLockTest

		public void RecursiveReadLockTest ()
		{
			var v = new ReaderWriterLockSlim (LockRecursionPolicy.SupportsRecursion);

			Assert.IsTrue (v.TryEnterReadLock (100), "#1");
			Assert.IsTrue (v.TryEnterReadLock (100), "#2");
			Assert.IsTrue (v.TryEnterReadLock (100), "#3");

			Assert.AreEqual (3, v.RecursiveReadCount);
		}
开发者ID:cthrax,项目名称:mono,代码行数:10,代码来源:ReaderWriterLockSlimTest.cs

示例9: 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));
		}
开发者ID:cthrax,项目名称:mono,代码行数:22,代码来源:ReaderWriterLockSlimTest.cs

示例10: RecursiveWriteReadAcquisitionInterleaving

        public void RecursiveWriteReadAcquisitionInterleaving()
        {
            var v = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);

            v.EnterWriteLock();
            Assert.IsTrue(v.IsWriteLockHeld, "#1");

            bool result = true;
            var t = new Thread(delegate ()
            {
                result = v.TryEnterReadLock(100);
            });
            t.Start();
            t.Join();
            Assert.IsFalse(result, "#2");

            v.ExitWriteLock();
            t = new Thread(delegate ()
            {
                result = v.TryEnterReadLock(100);
            });
            t.Start();
            t.Join();
            Assert.IsTrue(result, "#3");
        }
开发者ID:mesheets,项目名称:Theraot-CF,代码行数:25,代码来源:ReaderWriterLockSlimTest.cs

示例11: 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();
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:53,代码来源:ReaderWriterLockSlimTests.cs

示例12: WriterToReaderChain

 public static void WriterToReaderChain()
 {
     using (AutoResetEvent are = new AutoResetEvent(false))
     using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim())
     {
         rwls.EnterWriteLock();
         Task t = Task.Factory.StartNew(() =>
         {
             Assert.False(rwls.TryEnterReadLock(TimeSpan.FromMilliseconds(10)));
             Task.Run(() => are.Set()); // ideally this won't fire until we've called EnterReadLock, but it's a benign race in that the test will succeed either way
             rwls.EnterReadLock();
             rwls.ExitReadLock();
         }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
         are.WaitOne();
         rwls.ExitWriteLock();
         t.GetAwaiter().GetResult();
     }
 }
开发者ID:ESgarbi,项目名称:corefx,代码行数:18,代码来源:ReaderWriterLockSlimTests.cs

示例13: WritersAreMutuallyExclusiveFromReaders

 public static void WritersAreMutuallyExclusiveFromReaders()
 {
     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.TryEnterReadLock(0));
                 Assert.False(rwls.IsReadLockHeld);
                 barrier.SignalAndWait();
             }));
     }
 }
开发者ID:ESgarbi,项目名称:corefx,代码行数:23,代码来源:ReaderWriterLockSlimTests.cs

示例14: 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)));
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:13,代码来源:ReaderWriterLockSlimTests.cs

示例15: QuickTryEnterReadLock

 /// <summary>
 /// 
 /// </summary>
 /// <param name="readerWriterLockSlim"></param>
 /// <returns></returns>
 public static bool QuickTryEnterReadLock( ReaderWriterLockSlim readerWriterLockSlim )
 {
     return readerWriterLockSlim.TryEnterReadLock( s_TimeSpan );
 }
开发者ID:andyhebear,项目名称:HappyQ-WowServer,代码行数:9,代码来源:ReaderWriterLockSlimEx.cs


注:本文中的System.Threading.ReaderWriterLockSlim.TryEnterReadLock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。