public void TryAcquireLock_HasLock_ReturnsTrue() { SimpleFileLock fileLock = SimpleFileLock.Create("SimpleFileLockTests"); Assert.That(fileLock.TryAcquireLock(), Is.True); Assert.That(fileLock.TryAcquireLock(), Is.True); fileLock.ReleaseLock(); }
/// <summary> /// First, we try to get the token quickly and quietly. /// If that fails, we put up a dialog and wait a number of seconds while we wait for the token to come free. /// </summary> /// <param name="uniqueIdentifier"></param> /// <param name="applicationName">Optional; used for user messaging</param> /// <param name="secondsToWait">Default = 10; Number of seconds the dialog will be displayed while waiting for the token to come free</param> /// <returns>True if we successfully acquired the token, false otherwise</returns> public static bool AcquireToken(string uniqueIdentifier, string applicationName = null, int secondsToWait = 10) { Guard.AgainstNull(uniqueIdentifier, "uniqueIdentifier"); bool tokenAcquired = AcquireTokenQuietly(uniqueIdentifier); if (tokenAcquired) { return(true); } string waitingMsg = applicationName == null? L10NSharp.LocalizationManager.GetString("Application.WaitingFinish.General", "Waiting for other application to finish...") : String.Format( L10NSharp.LocalizationManager.GetString("Application.WaitingFinish.Specific", "Waiting for other {0} to finish...", "{0} is the application name"), applicationName); using (var dlg = new SimpleMessageDialog(waitingMsg, applicationName)) { dlg.Show(); try { var timeoutTime = DateTime.Now.AddSeconds(secondsToWait); while (DateTime.Now < timeoutTime && !tokenAcquired) { tokenAcquired = s_fileLock.TryAcquireLock(); Thread.Sleep(500); } } catch (Exception e) { string errorMsg = applicationName == null? L10NSharp.LocalizationManager.GetString("Application.ProblemStarting.General", "There was a problem starting the application which might require that you restart your computer.") : String.Format( L10NSharp.LocalizationManager.GetString("Application.ProblemStarting.Specific", "There was a problem starting {0} which might require that you restart your computer.", "{0} is the application name"), applicationName); ErrorReport.NotifyUserOfProblem(e, errorMsg); } } if (!tokenAcquired) // cannot acquire { string errorMsg = applicationName == null? L10NSharp.LocalizationManager.GetString("Application.AlreadyRunning.General", "Another copy of the application is already running. If you cannot find it, restart your computer.") : String.Format( L10NSharp.LocalizationManager.GetString("Application.AlreadyRunning.Specific", "Another copy of {0} is already running. If you cannot find that copy of {0}, restart your computer.", "{0} is the application name"), applicationName); ErrorReport.NotifyUserOfProblem(errorMsg); return(false); } return(true); }
public void ReleaseLock_HasLock_LockFileDoesNotExist() { SimpleFileLock fileLock = SimpleFileLock.Create("SimpleFileLockTests"); Assert.That(fileLock.TryAcquireLock(), Is.True); fileLock.ReleaseLock(); Assert.That(File.Exists(LockPath), Is.False); }
public void TryAcquireLock_OldEmptyFileLock_ReturnsTrue() { File.WriteAllText(LockPath, ""); SimpleFileLock fileLock = SimpleFileLock.Create("SimpleFileLockTests"); Assert.That(fileLock.TryAcquireLock(), Is.True); fileLock.ReleaseLock(); }
public void TryAcquireLock_LockFileCurrentlyOpen_ReturnsFalse() { SimpleFileLock fileLock = SimpleFileLock.Create("SimpleFileLockTests"); using (File.Open(LockPath, FileMode.Create)) { Assert.That(fileLock.TryAcquireLock(), Is.False); } }
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); }
/// <summary> /// Try to acquire the token quietly /// </summary> /// <param name="uniqueIdentifier"></param> /// <returns>True if we successfully acquired the token, false otherwise</returns> public static bool AcquireTokenQuietly(string uniqueIdentifier) { Guard.AgainstNull(uniqueIdentifier, "uniqueIdentifier"); // Each process may only acquire one token if (s_fileLock != null) { return(false); } s_fileLock = SimpleFileLock.Create(uniqueIdentifier + FileExtension); return(s_fileLock.TryAcquireLock()); }
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(); }