本文整理汇总了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();
}
示例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;
}
示例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();
}
}
}
示例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 ();
}
示例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 ();
}
示例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_;
}
示例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);
}
示例8: LockingObject
public LockingObject(ReaderWriterLock _lock, AccessMode _lockMode)
{
readerWriterLock = _lock;
accessMode = _lockMode;
if (accessMode == AccessMode.Read)
{
readerWriterLock.AcquireReaderLock(-1);
}
else
{
readerWriterLock.AcquireWriterLock(-1);
}
}
示例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;
}
}
示例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();
});
}
示例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);
}
}
示例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); }
}
示例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();
}
}
示例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();
}
}
示例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);
}