本文整理汇总了C#中System.Threading.LockCookie类的典型用法代码示例。如果您正苦于以下问题:C# LockCookie类的具体用法?C# LockCookie怎么用?C# LockCookie使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LockCookie类属于System.Threading命名空间,在下文中一共展示了LockCookie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Animate
public static void Animate(Image image, EventHandler onFrameChangedHandler)
{
if (image != null)
{
ImageInfo item = null;
lock (image)
{
item = new ImageInfo(image);
}
StopAnimate(image, onFrameChangedHandler);
bool isReaderLockHeld = rwImgListLock.IsReaderLockHeld;
LockCookie lockCookie = new LockCookie();
threadWriterLockWaitCount++;
try
{
if (isReaderLockHeld)
{
lockCookie = rwImgListLock.UpgradeToWriterLock(-1);
}
else
{
rwImgListLock.AcquireWriterLock(-1);
}
}
finally
{
threadWriterLockWaitCount--;
}
try
{
if (item.Animated)
{
if (imageInfoList == null)
{
imageInfoList = new List<ImageInfo>();
}
item.FrameChangedHandler = onFrameChangedHandler;
imageInfoList.Add(item);
if (animationThread == null)
{
animationThread = new Thread(new ThreadStart(ImageAnimator.AnimateImages50ms));
animationThread.Name = typeof(ImageAnimator).Name;
animationThread.IsBackground = true;
animationThread.Start();
}
}
}
finally
{
if (isReaderLockHeld)
{
rwImgListLock.DowngradeFromWriterLock(ref lockCookie);
}
else
{
rwImgListLock.ReleaseWriterLock();
}
}
}
}
示例2: WriteLock
/// <summary>
/// Execute the action under a write lock
/// </summary>
/// <param name="cacheAction">The cache action.</param>
public void WriteLock(CacheAction cacheAction)
{
if(readerWriterLock.IsWriterLockHeld)
{
cacheAction();
return;
}
bool readerLockHeld = readerWriterLock.IsReaderLockHeld;
LockCookie writerLock = new LockCookie();
if (readerLockHeld)
{
writerLock = readerWriterLock.UpgradeToWriterLock(Timeout.Infinite);
}
else
readerWriterLock.AcquireWriterLock(Timeout.Infinite);
try
{
cacheAction();
}
finally
{
if (readerLockHeld)
readerWriterLock.DowngradeFromWriterLock(ref writerLock);
else
readerWriterLock.ReleaseWriterLock();
}
}
示例3: Equals
public bool Equals (LockCookie obj)
{
if (this.ThreadId == obj.ThreadId &&
this.ReaderLocks == obj.ReaderLocks &&
this.WriterLocks == obj.WriterLocks) {
return(true);
} else {
return(false);
}
}
示例4: Remove
public void Remove(IPAddr ip)
{
try
{
locker.AcquireReaderLock(new TimeSpan(0, 1, 0));
try
{
if (list.ContainsKey(ip))
{
LockCookie lc = new LockCookie();
try
{
lc = locker.UpgradeToWriterLock(new TimeSpan(0, 1, 0));
try
{
list.Remove(ip);
}
finally
{
locker.DowngradeFromWriterLock(ref lc);
}
}
catch (ApplicationException e)
{
Logging.LogCenter.Instance.LogException(e);
}
}
}
finally
{
locker.ReleaseReaderLock();
}
}
catch (ApplicationException aex)
{
Logging.LogCenter.Instance.LogException(aex);
}
}
示例5: RegisterHook
public static void RegisterHook(ISkinHook client)
{
if (m_Clients.Contains(client))
return;
if (!m_FilterOnThread.ContainsKey(System.Threading.Thread.CurrentThread.GetHashCode()))
HookThread();
LockCookie cookie1 = new LockCookie();
bool readerLockHeld = NonClientHook.rwClientsListLock.IsReaderLockHeld;
if (readerLockHeld)
{
cookie1 = NonClientHook.rwClientsListLock.UpgradeToWriterLock(-1);
}
else
{
NonClientHook.rwClientsListLock.AcquireWriterLock(-1);
}
try
{
m_Clients.Add(client);
}
finally
{
if (readerLockHeld)
{
NonClientHook.rwClientsListLock.DowngradeFromWriterLock(ref cookie1);
}
else
{
NonClientHook.rwClientsListLock.ReleaseWriterLock();
}
}
}
示例6: UpgradeToWriterLock
public LockCookie UpgradeToWriterLock(int millisecondsTimeout)
{
LockCookie result = new LockCookie();
this.FCallUpgradeToWriterLock(ref result, millisecondsTimeout);
return result;
}
示例7: RestoreLockInternal
private extern void RestoreLockInternal(ref LockCookie lockCookie);
示例8: RestoreLock
public void RestoreLock(ref LockCookie lockCookie)
{
this.RestoreLockInternal(ref lockCookie);
}
示例9: GetLockCookie
LockCookie GetLockCookie ()
{
LockCookie cookie = new LockCookie (Thread.CurrentThreadId);
if (HasWriterLock())
cookie.WriterLocks = -state;
else {
object locks = reader_locks [Thread.CurrentThreadId];
if (locks != null) cookie.ReaderLocks = (int)locks;
}
return cookie;
}
示例10: RestoreLock
public void RestoreLock(ref LockCookie lockCookie)
{
lock (this) {
if (lockCookie.WriterLocks != 0)
AcquireWriterLock (-1, lockCookie.WriterLocks);
else if (lockCookie.ReaderLocks != 0)
AcquireReaderLock (-1, lockCookie.ReaderLocks);
}
}
示例11: DowngradeFromWriterLock
public void DowngradeFromWriterLock(ref LockCookie lockCookie)
{
lock (this) {
if (!HasWriterLock())
throw new ApplicationException ("The thread does not have the writer lock.");
state = lockCookie.ReaderLocks;
reader_locks [Thread.CurrentThreadId] = state;
if (readers > 0) {
Monitor.PulseAll (this);
}
// MSDN: A thread does not block when downgrading from the writer lock,
// even if other threads are waiting for the writer lock
}
}
示例12: RestoreLock
public void RestoreLock(ref LockCookie lockCookie)
{
}
示例13: FCallReleaseLock
private extern void FCallReleaseLock(ref LockCookie result);
示例14: ReleaseLock
public LockCookie ReleaseLock()
{
LockCookie result = new LockCookie();
this.FCallReleaseLock(ref result);
return result;
}
示例15: Load
public bool Load()
{
try
{
LockCookie upgrade = new LockCookie();
bool upgraded = false;
if (locker.IsReaderLockHeld)
{
upgrade = locker.UpgradeToWriterLock(new TimeSpan(0, 1, 0));
upgraded = true;
}
else
locker.AcquireWriterLock(new TimeSpan(0, 1, 0));
try
{
try
{
if (File.Exists(ConfigurationManagement.Instance.ConfigurationPath + Path.DirectorySeparatorChar + "IPLists.cfg"))
{
XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, IPList>));
TextReader reader = new StreamReader(ConfigurationManagement.Instance.ConfigurationPath + Path.DirectorySeparatorChar + "IPLists.cfg");
iplists = (SerializableDictionary<string, IPList>)serializer.Deserialize(reader);
reader.Close();
reader.Dispose();
}
else
{
iplists = new SerializableDictionary<string,IPList>();
}
}
catch (Exception e)
{
Logging.LogCenter.Instance.LogException(e);
iplists = new SerializableDictionary<string, IPList>();
}
}
finally
{
if (upgraded)
locker.DowngradeFromWriterLock(ref upgrade);
else
locker.ReleaseWriterLock();
}
return true;
}
catch (ApplicationException ex)
{
Logging.LogCenter.Instance.LogException(ex);
return false;
}
}