Process BuildProcess() { string pypath; try { pypath = PythonHelper.FindPreferredPython(); } catch { LoggingService.LogError("Cannot locate python executable. Disabling python parsing."); return(null); } var process = new Process(); process.StartInfo.FileName = pypath; process.StartInfo.EnvironmentVariables ["WATCH_PID"] = Process.GetCurrentProcess().Id.ToString(); process.StartInfo.Arguments = "-u -"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; var started = false; process.OutputDataReceived += delegate(object o, DataReceivedEventArgs e) { if (!started) { if (e.Data.Trim().StartsWith("Listening on port ")) { Monitor.Enter(m_syncRoot); m_Port = Int32.Parse(e.Data.Substring(18).Trim()); started = true; m_ProcessSuccess = true; Monitor.Pulse(m_syncRoot); Monitor.Exit(m_syncRoot); Process oldProcess = null; // Cycle to the new process do { oldProcess = m_Process; } while (Interlocked.CompareExchange <Process> (ref m_Process, process, oldProcess) != oldProcess); Interlocked.Exchange(ref m_sinceCycle, 0); // kill old process after 5 seconds if (oldProcess != null) { GLib.Timeout.Add(5000, delegate { oldProcess.Kill(); return(false); }); } } } }; return(process); }