/// <summary> /// Attempts to aquire a lock to access the section of code (non-blocking). /// (Note: You must check the returned LockToken in this case to ensure that /// the lock was actually aquired.) /// </summary> /// <returns>LockToken object</returns> public bool TryAquireLock(out LockToken lockToken) { if (CMonitor.TryEnter(_monitorObject)) { lockToken = new LockToken(this); return(true); } lockToken = default(LockToken); return(false); }
/// <summary> /// Aquire a lock and return the LockToken when the lock is aquired. /// </summary> /// <param name="waitTime">Amount of time to wait to aquire the lock in milliseconds</param> /// <returns>LockToken object</returns> public LockToken AquireLock(int waitTime) { var sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds < waitTime) { if (CMonitor.TryEnter(_monitorObject)) { return(new LockToken(this)); } _delayEvent.Wait(100); } return(new LockToken(null)); }
public bool TryEnter() { return(CMonitor.TryEnter(mutex)); }