Ejemplo n.º 1
0
        /// <summary>
        /// Executes a python command
        /// </summary>
        public static ProcessOutput ExecutePythonCommand(string pythonArgs, int timeout = 300000)
        {
            var output = new ProcessOutput();

            using (var process = new Process())
            {
                process.StartInfo.FileName  = "python.exe";
                process.StartInfo.Arguments = pythonArgs;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                //Hide the python window if possible
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle     = ProcessWindowStyle.Hidden;
                process.StartInfo.CreateNoWindow  = true;


                using (var outputWaitHandle = new AutoResetEvent(false))
                    using (var errorWaitHandle = new AutoResetEvent(false))
                    {
                        process.OutputDataReceived += (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                try
                                {
                                    outputWaitHandle.Set();
                                }
                                catch
                                {
                                    //probably is already disposed
                                }
                            }
                            else
                            {
                                output.OutputText.AppendLine(e.Data);
                            }
                        };
                        process.ErrorDataReceived += (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                try
                                {
                                    errorWaitHandle.Set();
                                }
                                catch
                                {
                                    //probably is already disposed
                                }
                            }
                            else
                            {
                                output.OutputText.AppendLine(e.Data);
                            }
                        };

                        process.Start();

                        process.BeginOutputReadLine();
                        process.BeginErrorReadLine();

                        if (process.WaitForExit(timeout) &&
                            outputWaitHandle.WaitOne(timeout) &&
                            errorWaitHandle.WaitOne(timeout))
                        {
                            // Process completed.
                            output.ExitCode = process.ExitCode;
                        }
                        else
                        {
                            // Timed out.
                            output.ExitCode = -1;
                        }
                    }
            }
            return(output);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes a python command
        /// </summary>
        public static ProcessOutput ExecutePythonCommand(string pythonArgs, int timeout = 300000)
        {
            var output = new ProcessOutput();
            using (var process = new Process())
            {
                process.StartInfo.FileName = "python.exe";
                process.StartInfo.Arguments = pythonArgs;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                //Hide the python window if possible
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.StartInfo.CreateNoWindow = true;

                using (var outputWaitHandle = new AutoResetEvent(false))
                using (var errorWaitHandle = new AutoResetEvent(false))
                {
                    process.OutputDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            try
                            {
                                outputWaitHandle.Set();
                            }
                            catch
                            {
                                //probably is already disposed
                            }
                        }
                        else
                        {
                            output.OutputText.AppendLine(e.Data);
                        }
                    };
                    process.ErrorDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            try
                            {
                                errorWaitHandle.Set();
                            }
                            catch
                            {
                                //probably is already disposed
                            }
                        }
                        else
                        {
                            output.OutputText.AppendLine(e.Data);
                        }
                    };

                    process.Start();

                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    if (process.WaitForExit(timeout) &&
                        outputWaitHandle.WaitOne(timeout) &&
                        errorWaitHandle.WaitOne(timeout))
                    {
                        // Process completed.
                        output.ExitCode = process.ExitCode;
                    }
                    else
                    {
                        // Timed out.
                        output.ExitCode = -1;
                    }
                }
            }
            return output;
        }