private Process Launch(LaunchStrategy strategy) { Logger.Current.Trace("Launching configuration {0} with strategy {1}", Configuration.identifier, strategy); switch (strategy) { case LaunchStrategy.AttachOrLaunch: try { return(Launch(LaunchStrategy.Attach)); } catch (Exception e) { Logger.Current.Trace(e); try { return(Launch(LaunchStrategy.Launch)); } catch (Exception ex) { throw ex; } } case LaunchStrategy.Launch: Logger.Current.Trace("Launching path={0} args={1}", Configuration.appPath, Configuration.arguments); this.LaunchStrategy = LaunchStrategy.Launch; Process process = Process.Start(Configuration.appPath, Configuration.arguments); Factory.LockProcess(process); return(process); case LaunchStrategy.Attach: FileRef runningApp = new FileRef(Configuration.appPath); foreach (Process candidate in Process.GetProcesses()) { if (candidate.Id == 0) // idle process { continue; } try { if (runningApp.Equals(new FileRef(candidate)) && Factory.TryLockProcess(candidate)) { Logger.Current.Info("Attaching to process {0} for configuration {1}: {2}", candidate.Id, Configuration.identifier, candidate.MainModule.FileName); this.LaunchStrategy = LaunchStrategy.Attach; return(candidate); } } catch (Exception) { // couldn't read the process, e.g. svchost } } Logger.Current.Info("Couldn't find a process to attach to for configuration {0} with path {1}", Configuration.identifier, Configuration.appPath); throw new Exception("Could not find any process with path " + Configuration.appPath + " to bind to"); } throw new Exception("Unknown LaunchStrategy " + strategy); }
private Process Launch(LaunchStrategy strategy) { Logger.Current.Trace("Launching configuration {0} with strategy {1}", Configuration.identifier, strategy); switch (strategy) { case LaunchStrategy.AttachOrLaunch: try { return Launch(LaunchStrategy.Attach); } catch (Exception e) { Logger.Current.Trace(e); try { return Launch(LaunchStrategy.Launch); } catch (Exception ex) { throw ex; } } case LaunchStrategy.Launch: Logger.Current.Trace("Launching path={0} args={1}", Configuration.appPath, Configuration.arguments); this.LaunchStrategy = LaunchStrategy.Launch; Process process = Process.Start(Configuration.appPath, Configuration.arguments); Factory.LockProcess(process); return process; case LaunchStrategy.Attach: FileRef runningApp = new FileRef(Configuration.appPath); foreach (Process candidate in Process.GetProcesses()) { if (candidate.Id == 0) // idle process continue; try { if (runningApp.Equals(new FileRef(candidate)) && Factory.TryLockProcess(candidate)) { Logger.Current.Info("Attaching to process {0} for configuration {1}: {2}", candidate.Id, Configuration.identifier, candidate.MainModule.FileName); this.LaunchStrategy = LaunchStrategy.Attach; return candidate; } } catch (Exception) { // couldn't read the process, e.g. svchost } } Logger.Current.Info("Couldn't find a process to attach to for configuration {0} with path {1}", Configuration.identifier, Configuration.appPath); throw new Exception("Could not find any process with path " + Configuration.appPath + " to bind to"); } throw new Exception("Unknown LaunchStrategy " + strategy); }