public void Start() { if (OnProcessStart != null) { OnProcessStart.Invoke(this); } }
private bool Launch(string args, OutputTo outputTo) { outputLog = ""; if (binary == null) { throw new Exception("Launch(): No executable set, forgot to call SetExecuteable()?\r\n"); } processStartInfo.FileName = binary; processStartInfo.Arguments = args; execProcess.StartInfo = processStartInfo; try { execProcess.Start(); } catch (Exception ex) { MsgBox.error("Unable to start process for console output\r\n", ex); return(false); } OnProcessStart?.Invoke(this, EventArgs.Empty); if (outputTo == OutputTo.Console) { if (execProcess.StartInfo.RedirectStandardError) { stderrThread = new Thread(() => ThreadConsoleUpdate(execProcess.StandardError)); stderrThread.IsBackground = true; stderrThread.Start(); } if (execProcess.StartInfo.RedirectStandardOutput) { stdoutThread = new Thread(() => ThreadConsoleUpdate(execProcess.StandardOutput)); stdoutThread.IsBackground = true; stdoutThread.Start(); } } if (outputTo == OutputTo.Log) { execProcess.OutputDataReceived += new DataReceivedEventHandler(OutputLogHandler); execProcess.ErrorDataReceived += new DataReceivedEventHandler(ErrorLogHandler); processOutputStreamOpen = true; processErrorStreamOpen = true; execProcess.BeginOutputReadLine(); execProcess.BeginErrorReadLine(); } else { processOutputStreamOpen = false; processErrorStreamOpen = false; } return(true); }
private void ProcWatcher(object sender, DoWorkEventArgs e) { Dictionary <int, MonkeyProc> knownProcs = new Dictionary <int, MonkeyProc>(); foreach (MonkeyProc p in MonkeyProc.GetProcesses()) { knownProcs.Add(p.Id, p); } while (!Cancel) { Thread.Sleep(100); // don't hog all the cpu var current = MonkeyProc.GetProcesses(); var closed = knownProcs.Values.Except(current, MonkeyProc.ProcessComparer).ToArray(); // 3-21-15 Again, make a copy. The foreach iterator won't be const otherwise if (OnProcessExit == null) { foreach (MonkeyProc p in closed) { knownProcs.Remove(p.Id); } } else { foreach (MonkeyProc p in closed) { OnProcessExit.Invoke(p, EventArgs.Empty); knownProcs.Remove(p.Id); } } if (OnProcessStart != null) { foreach (MonkeyProc p in current) { if (!knownProcs.ContainsKey(p.Id)) { OnProcessStart.Invoke(p, EventArgs.Empty); knownProcs.Add(p.Id, p); } } } } e.Cancel = true; }