public bool ReleaseLock() { //Need to own the lock in order to release it (and we can reacquire the lock inside the current process) if (LockIO.LockExists(LockFilePath) && TryAcquireLock()) { LockIO.DeleteLock(LockFilePath); } return(true); }
public static SimpleFileLock Create(string lockName, [Optional] TimeSpan lockTimeout) { if (string.IsNullOrEmpty(lockName)) { throw new ArgumentNullException("lockName", "lockName cannot be null or emtpy."); } return(new SimpleFileLock(lockName, lockTimeout) { LockFilePath = LockIO.GetFilePath(lockName) }); }
public void TryAcquireLock_LockedByActiveProcess_ReturnsFalse() { LockIO.WriteLock(LockPath, new FileLockContent { PID = ActiveProcessId, ProcessName = GetSafeActiveProcessName(), Timestamp = DateTime.Now.Ticks }); SimpleFileLock fileLock = SimpleFileLock.Create("SimpleFileLockTests"); Assert.That(fileLock.TryAcquireLock(), Is.False); }
public void TryAcquireLock_LockedByActiveProcessTimedOut_ReturnsTrue() { LockIO.WriteLock(LockPath, new FileLockContent { PID = ActiveProcessId, ProcessName = GetSafeActiveProcessName(), Timestamp = (DateTime.Now - TimeSpan.FromHours(2)).Ticks }); SimpleFileLock fileLock = SimpleFileLock.Create("SimpleFileLockTests", TimeSpan.FromHours(1)); Assert.That(fileLock.TryAcquireLock(), Is.True); }
public void TryAcquireLock_LockedByDeadProcess_ReturnsTrue() { LockIO.WriteLock(LockPath, new FileLockContent { PID = 9999, ProcessName = Path.GetRandomFileName(), Timestamp = DateTime.Now.Ticks }); SimpleFileLock fileLock = SimpleFileLock.Create("SimpleFileLockTests"); Assert.That(fileLock.TryAcquireLock(), Is.True); fileLock.ReleaseLock(); }
public void ReleaseLock_LockedByActiveProcess_LockFileExists() { LockIO.WriteLock(LockPath, new FileLockContent { PID = ActiveProcessId, ProcessName = GetSafeActiveProcessName(), Timestamp = DateTime.Now.Ticks }); SimpleFileLock fileLock = SimpleFileLock.Create("SimpleFileLockTests"); fileLock.ReleaseLock(); Assert.That(File.Exists(LockPath), Is.True); }
public bool TryAcquireLock() { if (LockIO.LockExists(LockFilePath)) { var lockContent = LockIO.ReadLock(LockFilePath); //Someone else owns the lock if (lockContent.GetType() == typeof(OtherProcessOwnsFileLockContent)) { return(false); } //the file no longer exists if (lockContent.GetType() == typeof(MissingFileLockContent)) { return(AcquireLock()); } //This lock belongs to this process - we can reacquire the lock if (lockContent.PID == Process.GetCurrentProcess().Id) { return(AcquireLock()); } //The process which created it is no longer running if (!ProcessIsRunning((int)lockContent.PID, lockContent.ProcessName)) { return(AcquireLock()); } if (LockTimeout != TimeSpan.Zero) { var lockWriteTime = new DateTime(lockContent.Timestamp); if (!(Math.Abs((DateTime.Now - lockWriteTime).TotalSeconds) > LockTimeout.TotalSeconds)) { return(false); //The lock has not timed out - we can't acquire it } } else { return(false); } } //Acquire the lock return(AcquireLock()); }
public bool TryAcquireLock() { if (LockIO.LockExists(LockFilePath)) { var lockContent = LockIO.ReadLock(LockFilePath); //Someone else owns the lock if (lockContent.GetType() == typeof(OtherProcessOwnsFileLockContent)) { return(false); } //the file no longer exists if (lockContent.GetType() == typeof(MissingFileLockContent)) { return(AcquireLock()); } var lockWriteTime = new DateTime(lockContent.Timestamp); //This lock belongs to this process - we can reacquire the lock var currentProcess = Process.GetCurrentProcess(); if (lockContent.PID == currentProcess.Id && lockContent.MachineName == Environment.MachineName) { return(AcquireLock()); } //The lock has not timed out - we can't acquire it if (!(Math.Abs((DateTime.Now - lockWriteTime).TotalSeconds) > LockTimeout.TotalSeconds)) { return(false); } } //Acquire the lock return(AcquireLock()); }
private bool AcquireLock() { return(LockIO.WriteLock(LockFilePath, CreateLockContent())); }