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


C# ReaderWriterLock.AcquireWriterLock方法代码示例

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


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

示例1: LockOnReaderWriterLock

        private static void LockOnReaderWriterLock()
        {
            Console.WriteLine("About to lock on the ReaderWriterLock. Debug after seeing \"Signaled to acquire the reader lock.\"");
            ReaderWriterLock rwLock = new ReaderWriterLock();

            ManualResetEvent rEvent = new ManualResetEvent(false);
            ManualResetEvent pEvent = new ManualResetEvent(false);

            ThreadPool.QueueUserWorkItem((object state) =>
            {
                rwLock.AcquireWriterLock(-1);
                Console.WriteLine("Writer lock acquired!");
                rEvent.Set();
                pEvent.WaitOne();
            });

            rEvent.WaitOne();

            Console.WriteLine("Signaled to acquire the reader lock.");

            rwLock.AcquireReaderLock(-1);

            Console.WriteLine("Reader lock acquired");

            pEvent.Set();

            Console.WriteLine("About to end the program. Press any key to exit.");
            Console.ReadKey();
        }
开发者ID:alienwaredream,项目名称:toolsdotnet,代码行数:29,代码来源:Program.cs

示例2: getOneCell

        public static string getOneCell()
        {
            string encodedOrder=null;
             readSem.WaitOne();// wait until buffer cell is not empty then only read
            ReaderWriterLock rw=new ReaderWriterLock();
            rw.AcquireWriterLock(Timeout.Infinite);
            try{
            //reading from buffer --- ciruclar queue

            if (front_read == bufferSize - 1)
                 front_read = 0;
            else
                 {

                     front_read = front_read + 1;
                  }
               encodedOrder = buffer[front_read];
               buffer[front_read] = string.Empty;
               String[] arr=encodedOrder.Split('#');
               Console.WriteLine("  chicken farm got an order from {0} of {1} chicken",arr[0],arr[2]);
            }
            catch (Exception e){ Console.WriteLine(""); }
            finally
            {
            rw.ReleaseWriterLock();
            writeSem.Release();

            }

            return encodedOrder;
        }
开发者ID:jvutukur,项目名称:DistributedSoftwareDevelopmentProject,代码行数:31,代码来源:MultiBufferCell.cs

示例3: CreatLog

 /// <summary>
 ///     创建日志文件
 /// </summary>
 private void CreatLog() {
     TextWriter logwrite = null;
     var writelock = new ReaderWriterLock();
     try {
         writelock.AcquireWriterLock(-1);
         var directoryInfo = m_fileinfo.Directory;
         if (directoryInfo != null && !directoryInfo.Exists) {
             var directory = m_fileinfo.Directory;
             if (directory != null) directory.Create();
         }
         logwrite = TextWriter.Synchronized(m_fileinfo.CreateText());
         logwrite.WriteLine("#------------------------------------------------------");
         logwrite.WriteLine("#     SYSTEM LOG                                  ");
         logwrite.WriteLine("#                                                      ");
         logwrite.WriteLine("#     Create at " + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "   ");
         logwrite.WriteLine("#                                                      ");
         logwrite.WriteLine("#------------------------------------------------------");
         logwrite.Close();
     }
     catch (Exception ex) {
         throw new Exception("创建系统日志文件出错!" + ex);
     }
     finally {
         writelock.ReleaseWriterLock();
         if (logwrite != null) {
             logwrite.Close();
         }
     }
 }
开发者ID:kangwl,项目名称:KANG.Frame,代码行数:32,代码来源:Log.cs

示例4: TestIsWriterLockHeld

		public void TestIsWriterLockHeld ()
		{
			rwlock = new ReaderWriterLock ();
			Assert ("a1", !rwlock.IsWriterLockHeld);
			rwlock.AcquireWriterLock (500);
			Assert ("a2", rwlock.IsWriterLockHeld);
			RunThread (new ThreadStart (IsWriterLockHeld_2));
			rwlock.ReleaseWriterLock ();
		}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:9,代码来源:ReaderWriterLockTest.cs

示例5: TestIsWriterLockHeld

		public void TestIsWriterLockHeld ()
		{
			rwlock = new ReaderWriterLock ();
			Assert.IsTrue (!rwlock.IsWriterLockHeld, "#1");
			rwlock.AcquireWriterLock (500);
			Assert.IsTrue (rwlock.IsWriterLockHeld, "#2");
			RunThread (new ThreadStart (IsWriterLockHeld_2));
			rwlock.ReleaseWriterLock ();
		}
开发者ID:nlhepler,项目名称:mono,代码行数:9,代码来源:ReaderWriterLockTest.cs

示例6: WriterLockDisposer

			/// <summary>
			/// Acquires a writer lock on the list passed
			/// as argument.
			/// </summary>
			/// <param name="list"></param>
			public WriterLockDisposer(ReaderWriterLock lock_)
			{
				if (null == lock_)
				{
					throw new ArgumentNullException("lock_");
				}				

				lock_.AcquireWriterLock(LockTimeout);
				_lock = lock_;
			}
开发者ID:bamboo,项目名称:Bamboo.Prevalence,代码行数:15,代码来源:SerializableReaderWriterLock.cs

示例7: CantAqureLock

 public void CantAqureLock()
 {
     var l = new ReaderWriterLock();
     var e = new AutoResetEvent(false);
     var t = new Thread(() =>
     {
         l.AcquireWriterLock(TimeSpan.FromSeconds(1));
         e.Set();
         Thread.Sleep(TimeSpan.FromMinutes(10));
     });
     t.Start();
     e.WaitOne();
     var a = new DisposableReaderWriterLock(l, TimeSpan.FromSeconds(3), LockMode.Write);
 }
开发者ID:sergey-prokofiev,项目名称:sp-tools,代码行数:14,代码来源:DisposableReaderWriterLockTests.cs

示例8: LockingObject

        public LockingObject(ReaderWriterLock _lock, AccessMode _lockMode)
        {
            readerWriterLock = _lock;
            accessMode = _lockMode;

            if (accessMode == AccessMode.Read)
            {
                readerWriterLock.AcquireReaderLock(-1);
            }
            else
            {
                readerWriterLock.AcquireWriterLock(-1);
            }
        }
开发者ID:wxgb9801,项目名称:Measurer,代码行数:14,代码来源:LockingObject.cs

示例9: WriteLock

        private WriteLock( ReaderWriterLock syncLock, TimeSpan timeout )
        {
            if( syncLock == null )
                throw new ArgumentNullException( "ReaderWriterLock syncLock" );

            _rwLock = syncLock;

            try
            {
                syncLock.AcquireWriterLock( timeout );
            }
            catch( ApplicationException )
            {
                _timedOut = true;
            }
        }
开发者ID:onesimoh,项目名称:Andamio,代码行数:16,代码来源:WriteLock.cs

示例10: LockVSReaderWriterLockVSReaderWriterLockSlimVSHandle

		public void LockVSReaderWriterLockVSReaderWriterLockSlimVSHandle()
		{
			TestName("Lock vs. ReaderWriterLock vs. ReaderWriterLockSlim vs. Handle-based");

			Object o = new Object();
			
			CodeTimer.Time("Lock perf: Monitor", iterations,
			   () =>
			   {
				   Monitor.Enter(o);
				   //
				   Monitor.Exit(o);
			   });

			CodeTimer.Time("Lock perf: Lock", iterations,
			   () =>
			   {
				   lock (o)
				   {
				   }
				  
			   });

			ReaderWriterLockSlim rwls = new ReaderWriterLockSlim();
			CodeTimer.Time("Lock perf: ReaderWriterLockSlim", iterations,
			   () =>
			   {
				   rwls.EnterWriteLock();
				   rwls.ExitWriteLock();
			   });

			ReaderWriterLock rwl = new ReaderWriterLock();
			CodeTimer.Time("Lock perf: ReaderWriterLock", iterations,
			   () =>
			   {
				   rwl.AcquireWriterLock(Timeout.Infinite);
				   rwl.ReleaseWriterLock();
			   });

			Mutex mutex = new Mutex();
			CodeTimer.Time("Lock perf: Mutex", iterations,
			   () =>
			   {
				   mutex.WaitOne();
				   mutex.ReleaseMutex();
			   });
		}
开发者ID:denisefan28,项目名称:Wind,代码行数:47,代码来源:ThreadingPerformanceTest.cs

示例11: IICLockRegion

        public IICLockRegion(ReaderWriterLock innerLock, IICLockMode mode, int millisecondTimeout)
        {
            _innerLock = innerLock;
            _lockMode = mode;
            _msTimeout = millisecondTimeout;

            switch (mode) {
                case IICLockMode.ReaderLock:
                    _innerLock.AcquireReaderLock(millisecondTimeout);
                    break;
                case IICLockMode.WriterLock:
                    _innerLock.AcquireWriterLock(millisecondTimeout);
                    break;
                default:
                    throw new NotSupportedException("Unexcepted LockMode: " + mode);
            }
        }
开发者ID:amwtke,项目名称:commonlibrary,代码行数:17,代码来源:IICLockRegion.cs

示例12: PurgeCache

        protected override void PurgeCache(object sender, System.Timers.ElapsedEventArgs e)
        {
            ReaderWriterLock readWriteLock = new ReaderWriterLock();
            //Öncelik olarak ortanın altında seçiyoruz. Çünkü öncelik ekleme, güncelleme gibi işlerde.
            //Varsın bi kaç saniye geç temizlensin cache
            Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
            if (!Monitor.TryEnter(isPurging))
                return;

            try
            {
                readWriteLock.AcquireWriterLock(500);
                try
                {
                    List<object> expiredItems = new List<object>();

                    foreach (CacheKey timedKey in timedStorage.Keys)
                    {
                        if (timedKey.ExpirationDate < e.SignalTime)
                        {
                            // Temizlenmesi için birnevi nesneler işaretlenir.
                            expiredItems.Add(timedKey.Key);
                        }
                        else
                            break;
                    }

                    foreach (object key in expiredItems)
                    {
                        CacheKey timedKey = timedStorageIndex[key];
                        timedStorageIndex.Remove(timedKey.Key);
                        timedStorage.Remove(timedKey);
                    }
                }
                catch (ApplicationException ae)
                {
                    System.Console.WriteLine("Şu an lock lı.");
                }
                finally
                {
                    readWriteLock.ReleaseWriterLock();
                }
            }
            finally { Monitor.Exit(isPurging); }
        }
开发者ID:erdonet,项目名称:FrameworkWithDesingPatterns,代码行数:45,代码来源:ExpritionMemCache.cs

示例13: setOneCell

        public static void setOneCell(string encodedObject)
        {
            writeSem.WaitOne(); //wait until buffer has atleast one space in it
            ReaderWriterLock rw = new ReaderWriterLock();
            rw.AcquireWriterLock(Timeout.Infinite);
            try{      // writing into buffer --- circular queue
                if(rear_write==bufferSize-1)
                    rear_write=0;
                else
                    rear_write=rear_write+1;

                buffer[rear_write]=encodedObject;

            }
            finally
            {
                rw.ReleaseWriterLock();
                readSem.Release();
            }
        }
开发者ID:jvutukur,项目名称:DistributedSoftwareDevelopmentProject,代码行数:20,代码来源:MultiBufferCell.cs

示例14: WriteLog

 /// <summary>
 /// 写入日志
 /// </summary>
 /// <param name="content"></param>
 public void WriteLog(string content)
 {
     ReaderWriterLock writelock = new ReaderWriterLock();
     TextWriter logwrite = null;
     try {
         writelock.AcquireWriterLock(-1);
         logwrite = TextWriter.Synchronized(m_fileinfo.AppendText());
         logwrite.Write("\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + content + "\r\n");
         logwrite.Close();
     }
     catch (Exception ex) {
         throw new Exception("记录日志发生错误" + ex.Message);
     }
     finally {
         if (logwrite != null) {
             logwrite.Close();
         }
         writelock.ReleaseWriterLock();
     }
 }
开发者ID:kangwl,项目名称:BootstrapWeb,代码行数:24,代码来源:Log.cs

示例15: TestReleaseRestoreWriterLock

		public void TestReleaseRestoreWriterLock ()
		{
			rwlock = new ReaderWriterLock ();
			rwlock.AcquireWriterLock (500);
			rwlock.AcquireWriterLock (500);
			Assert.IsTrue (rwlock.IsWriterLockHeld);
			
			LockCookie co = rwlock.ReleaseLock ();
			RunThread (new ThreadStart (AcquireLock_readerWorks));
			
			rwlock.RestoreLock (ref co);
			RunThread (new ThreadStart (AcquireLock_readerFails));
			
			rwlock.ReleaseWriterLock ();
			Assert.IsTrue (rwlock.IsWriterLockHeld);
			rwlock.ReleaseWriterLock ();
			Assert.IsTrue (!rwlock.IsWriterLockHeld);
		}
开发者ID:nlhepler,项目名称:mono,代码行数:18,代码来源:ReaderWriterLockTest.cs


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