/// <summary> /// Attempts to start a tor process /// </summary> /// <param name="behavior"><see cref="StartBehavior"/></param> /// <param name="windowStyle"><see cref="ProcessWindowStyle"/></param> /// <returns><see cref="CurrentProcess"/> handle if able to start</returns> public virtual Process Start( StartBehavior behavior = StartBehavior.ReturnExisting, ProcessWindowStyle windowStyle = ProcessWindowStyle.Hidden) { IEnumerable <Process> existings = GetExisting(); if (existings != null && existings.Count() > 0) { switch (behavior) { case StartBehavior.ReturnExisting: return(CurrentProcess ?? existings.FirstOrDefault()); case StartBehavior.ThrowIfRunning: throw new InvalidOperationException("Tor is already running"); case StartBehavior.KillExistings: KillExisting(existings); break; default: throw new NotImplementedException(typeof(StartBehavior).Name + ":" + behavior); } } lock (_padlock) { if (_tor != null && !_tor.HasExited) { return(_tor); } var tor = new Process(); bool startOk = false; try { tor.StartInfo.FileName = _torPath.FullName; tor.StartInfo.Arguments = "-n"; tor.StartInfo.WindowStyle = windowStyle; startOk = tor.Start(); if (startOk) { return(_tor = tor); } else { tor.Dispose(); return(_tor = null); } } catch (Exception) { tor.Dispose(); return(_tor = null); } } }
/// <summary> /// Attempts to start a tor process /// </summary> /// <param name="behavior"><see cref="StartBehavior"/></param> /// <param name="windowStyle"><see cref="ProcessWindowStyle"/></param> /// <returns><see cref="CurrentProcess"/> handle if able to start</returns> public virtual Process Start( StartBehavior behavior = StartBehavior.ReturnExisting, ProcessWindowStyle windowStyle = ProcessWindowStyle.Hidden) { IEnumerable<Process> existings = GetExisting(); if(existings != null && existings.Count() > 0) { switch(behavior) { case StartBehavior.ReturnExisting: return CurrentProcess ?? existings.FirstOrDefault(); case StartBehavior.ThrowIfRunning: throw new InvalidOperationException("Tor is already running"); case StartBehavior.KillExistings: KillExisting(existings); break; default: throw new NotImplementedException(typeof(StartBehavior).Name + ":" + behavior); } } lock (_padlock) { if (_tor != null && !_tor.HasExited) return _tor; var tor = new Process(); bool startOk = false; try { tor.StartInfo.FileName = _torPath.FullName; tor.StartInfo.Arguments = "-n"; tor.StartInfo.WindowStyle = windowStyle; startOk = tor.Start(); if (startOk) return _tor = tor; else { tor.Dispose(); return _tor = null; } } catch (Exception) { tor.Dispose(); return _tor = null; } } }