public void process_is_killed_when_process_host_is_disposed() { uint processId; using (var subject = new ProcessHost("./ExampleInteractiveProcess.exe", Directory.GetCurrentDirectory())) { subject.Start(); processId = subject.ProcessId(); } Thread.Sleep(500); try { // system.diagnostics.process throws in debug mode, returns null in release mode! var prc = Process.GetProcessById((int)processId); Assert.That(prc, Is.Null); } catch (ArgumentException) { Assert.Pass(); } catch (InvalidOperationException) { Assert.Pass(); } Assert.Fail(); }
static int Main(string[] args) { if (args.Length > 0 && args[0] == "wait") { for(;;) { Thread.Sleep(1000); } } if (args.Length > 0 && args[0] == "print") { Console.WriteLine(string.Join(" ", args.Skip(1))); return 0; } if (args.Length > 0 && args[0] == "envarg") { var envargs = Environment.GetEnvironmentVariables(); foreach (DictionaryEntry envarg in envargs) { Console.WriteLine(envarg.Key + " = " + envarg.Value); } return 0; } if (args.Length > 0 && args[0] == "spawn") { // spawn a new process, and output its ID. Then wait forever var ph = new ProcessHost("ExampleNoninteractiveProcess.exe", ""); ph.StartAsChild("wait"); Console.WriteLine(ph.ProcessId()); for(;;) { Thread.Sleep(1000); } } if (args.Length > 1 && args[0] == "return") { return int.Parse(args[1]); } Console.WriteLine(StdOutMsg); using (var stdErr = Console.OpenStandardError()) { var msgBytes = Encoding.ASCII.GetBytes(StdErrMsg); stdErr.Write(msgBytes, 0, msgBytes.Length); } return 0; }
public void can_get_process_id_and_use_with_existing_dotnet_libraries() { using (var subject = new ProcessHost("./ExampleInteractiveProcess.exe", Directory.GetCurrentDirectory())) { subject.Start(); uint id = subject.ProcessId(); var process = Process.GetProcessById((int)id); Assert.That(process.HasExited, Is.False, "Exited"); process.Kill(); int exitCode; var exited = subject.WaitForExit(one_second, out exitCode); Assert.That(exited, Is.True, "Exited after kill"); Assert.That(exitCode, Is.EqualTo(0), "Exit code"); } }
public void Start() { _stopping = false; try { if (_startArgs.Contains("{0}")) { _dummyProcess = Call(Process.GetCurrentProcess().MainModule.FileName, "waitForPid " + Process.GetCurrentProcess().Id); _childProcess = CallAsChildUser(_target, string.Format(_startArgs, _dummyProcess.ProcessId())); } else { _childProcess = CallAsChildUser(_target, _startArgs); } if (_monitorThread == null || !_monitorThread.IsAlive) { _monitorThread = new Thread(() => MonitorChild(_childProcess)) { IsBackground = true }; _monitorThread.Start(); } if (_shouldLogOut || _shouldLogErr) { var tlogs = new Thread(() => WriteLogs(_childProcess)) {IsBackground = true}; tlogs.Start(); } } catch (Exception ex) { WriteWrapperFailure(ex); } }