public void TryLock_ExistingFile_NotLocked() { var dir = Path.Combine(OutputDir, "ExistingFile_NotLocked"); Directory.CreateDirectory(dir); var fileLock = new FileLock(dir); File.Create(fileLock.LockFile).Close(); Assert.IsTrue(File.Exists(fileLock.LockFile)); var locked = fileLock.TryLock(); Assert.IsTrue(locked); Assert.IsTrue(File.Exists(fileLock.LockFile)); fileLock.Dispose(); Assert.IsFalse(File.Exists(fileLock.LockFile)); }
public void TryLock_NoLock() { var dir = Path.Combine(OutputDir, "NoLock"); var fileLock = new FileLock(dir); if (File.Exists(fileLock.LockFile)) { File.Delete(fileLock.LockFile); } Assert.IsFalse(File.Exists(fileLock.LockFile)); var locked = fileLock.TryLock(); Assert.IsTrue(locked); Assert.IsTrue(File.Exists(fileLock.LockFile)); fileLock.Dispose(); Assert.IsFalse(File.Exists(fileLock.LockFile)); }
public void TryLock_AlreadyLocked() { var dir = Path.Combine(OutputDir, "AlreadyLocked"); var existingLock = new FileLock(dir); if (File.Exists(existingLock.LockFile)) { File.Delete(existingLock.LockFile); } Assert.IsFalse(File.Exists(existingLock.LockFile)); var locked = existingLock.TryLock(); Assert.IsTrue(locked); Assert.IsTrue(File.Exists(existingLock.LockFile)); var newLock = new FileLock(dir); var didLock = newLock.TryLock(); Assert.IsFalse(didLock); }
/// <summary> /// Try to establish the lock. /// </summary> /// <returns> /// True if the lock is now held by the caller; false if it is held /// by someone else. /// </returns> /// <exception cref="IOException"> /// the temporary output file could not be created. The caller /// does not hold the lock. /// </exception> public bool Lock() { _lockFile.Directory.Mkdirs(); if (_lockFile.Exists) { return(false); } try { _haveLock = true; _os = _lockFile.Create(); _fLck = FileLock.TryLock(_os, _lockFile); if (_fLck == null) { // We cannot use unlock() here as this file is not // held by us, but we thought we created it. We must // not delete it, as it belongs to some other process. _haveLock = false; try { _os.Close(); } catch (Exception) { // Fail by returning haveLck = false. } _os = null; } } catch (Exception) { Unlock(); throw; } return(_haveLock); }
private static LauncherErrorCode TryRun() { try { // Ensure to create parent of lock directory. Directory.CreateDirectory(EditorPath.DefaultTempPath); using (Mutex = FileLock.TryLock(Path.Combine(EditorPath.DefaultTempPath, "launcher.lock"))) { if (Mutex != null) { return(RunSingleInstance(false)); } MessageBox.Show("An instance of Stride Launcher is already running.", "Stride", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(LauncherErrorCode.ServerAlreadyRunning); } } catch (Exception e) { DisplayError($"Cannot start the instance of the Stride Launcher due to the following exception:\n{e.Message}"); return(LauncherErrorCode.UnknownError); } }
static int Main(string[] args) { var exeName = Path.GetFileName(Assembly.GetExecutingAssembly().Location); var showHelp = false; var windowsPhonePortMapping = false; int exitCode = 0; string logFileName = "routerlog.txt"; var p = new OptionSet { "Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) All Rights Reserved", "Xenko Router Server - Version: " + String.Format( "{0}.{1}.{2}", typeof(Program).Assembly.GetName().Version.Major, typeof(Program).Assembly.GetName().Version.Minor, typeof(Program).Assembly.GetName().Version.Build) + string.Empty, string.Format("Usage: {0} command [options]*", exeName), string.Empty, "=== Options ===", string.Empty, { "h|help", "Show this message and exit", v => showHelp = v != null }, { "log-file=", "Log build in a custom file (default: routerlog.txt).", v => logFileName = v }, { "register-windowsphone-portmapping", "Register Windows Phone IpOverUsb port mapping", v => windowsPhonePortMapping = true }, }; try { var commandArgs = p.Parse(args); if (showHelp) { p.WriteOptionDescriptions(Console.Out); return(0); } // Make sure path exists if (commandArgs.Count > 0) { throw new OptionException("This command expect no additional arguments", ""); } if (windowsPhonePortMapping) { WindowsPhoneTracker.RegisterWindowsPhonePortMapping(); return(0); } SetupTrayIcon(logFileName); // Enable file logging if (!string.IsNullOrEmpty(logFileName)) { var fileLogListener = new TextWriterLogListener(File.Open(logFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)); GlobalLogger.GlobalMessageLogged += fileLogListener; } // TODO: Lock will be only for this folder but it should be shared across OS (should we resolve XenkoDir?) using (var mutex = FileLock.TryLock("connectionrouter.lock")) { if (mutex == null) { Console.WriteLine("Another instance of Xenko Router is already running"); return(-1); } var router = new Router(); // Start router (in listen server mode) router.Listen(RouterClient.DefaultPort).Wait(); // Start Android management thread new Thread(() => AndroidTracker.TrackDevices(router)) { IsBackground = true }.Start(); // Start Windows Phone management thread new Thread(() => WindowsPhoneTracker.TrackDevices(router)) { IsBackground = true }.Start(); //Start iOS device discovery and proxy launcher //Currently this is used only internally for QA testing... as we cannot attach the debugger from windows for normal usages.. if (IosTracker.CanProxy()) { new Thread(async() => { var iosTracker = new IosTracker(router); await iosTracker.TrackDevices(); }) { IsBackground = true }.Start(); } // Start WinForms loop System.Windows.Forms.Application.Run(); } } catch (Exception e) { Console.WriteLine("{0}: {1}", exeName, e); if (e is OptionException) { p.WriteOptionDescriptions(Console.Out); } exitCode = 1; } return(exitCode); }
static int Main(string[] args) { var exeName = Path.GetFileName(Assembly.GetExecutingAssembly().Location); var showHelp = false; int exitCode = 0; string logFileName = "routerlog.txt"; var p = new OptionSet { "Copyright (c) 2018-2021 Stride and its contributors (https://stride3d.net)", "Copyright (c) 2011-2018 Silicon Studio Corp. (https://www.siliconstudio.co.jp)", "Stride Router Server - Version: " + Format("{0}.{1}.{2}", typeof(Program).Assembly.GetName().Version.Major, typeof(Program).Assembly.GetName().Version.Minor, typeof(Program).Assembly.GetName().Version.Build), Empty, $"Usage: {exeName} command [options]*", Empty, "=== Options ===", Empty, { "h|help", "Show this message and exit", v => showHelp = v != null }, { "log-file=", "Log build in a custom file (default: routerlog.txt).", v => logFileName = v } }; try { var commandArgs = p.Parse(args); if (showHelp) { p.WriteOptionDescriptions(Console.Out); return(0); } // Make sure path exists if (commandArgs.Count > 0) { throw new OptionException("This command expect no additional arguments.", ""); } // Enable file logging if (!string.IsNullOrEmpty(logFileName)) { var fileLogListener = new TextWriterLogListener(File.Open(logFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)); GlobalLogger.GlobalMessageLogged += fileLogListener; } // TODO: Lock will be only for this folder but it should be shared across OS using (var mutex = FileLock.TryLock("connectionrouter.lock")) { if (mutex is null) { Console.WriteLine("Another instance of Stride Router is already running."); return(-1); } var router = new Router(); // Start router (in listen server mode) router.Listen(RouterClient.DefaultPort).Wait(); // Start WinForms loop System.Windows.Forms.Application.Run(); } } catch (Exception ex) { Console.WriteLine("{0}: {1}", exeName, ex); if (ex is OptionException) { p.WriteOptionDescriptions(Console.Out); } exitCode = 1; } return(exitCode); }