Exemple #1
0
        private void ExecuteProcess(string cmd, string args,
                                    Action <string, CommandOutput> handleOutput)
        {
            var command = new CommandOutput
            {
                Executable = cmd,
                Arguments  = args,
                Lines      = new List <string>(),
                Raw        = new List <string>()
            };

            var info = new ProcessStartInfo
            {
                WorkingDirectory       = Directory.GetCurrentDirectory(),
                FileName               = cmd,
                Arguments              = args,
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                CreateNoWindow         = true
            };

            using (var process = new Process {
                StartInfo = info
            })
            {
                try
                {
                    process.Start();
                }
                catch (Exception ex)
                {
                    throw new BuildException(
                              string.Format("Error launching process: {0} {1}",
                                            cmd, args));
                }

                var handleProcessOutput = new Action <string>(text =>
                {
                    lock (this) { handleOutput(text, command); }
                });

                var io = new ProcessIoManager(process);
                io.StdoutTextRead += new StringReadEventHandler(handleProcessOutput);
                io.StderrTextRead += new StringReadEventHandler(handleProcessOutput);
                io.StartProcessOutputRead();

                process.WaitForExit();

                Thread.Sleep(100);

                process.Close();
                io.StopMonitoringProcessOutput();
            }

            Outputs.Add(command);
        }
Exemple #2
0
        private void ExecuteProcess(string cmd, string args,
            Action<string, CommandOutput> handleOutput)
        {
            var command = new CommandOutput
            {
                Executable = cmd,
                Arguments = args,
                Lines = new List<string>(),
                Raw = new List<string>()
            };

            var info = new ProcessStartInfo
            {
                WorkingDirectory = Directory.GetCurrentDirectory(),
                FileName = cmd,
                Arguments = args,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            };

            using (var process = new Process { StartInfo = info })
            {
                try
                {
                    process.Start();
                }
                catch (Exception ex)
                {
                    throw new BuildException(
                        string.Format("Error launching process: {0} {1}",
                        cmd, args));
                }

                var handleProcessOutput = new Action<string>(text =>
                {
                    lock (this) { handleOutput(text, command); }
                });

                var io = new ProcessIoManager(process);
                io.StdoutTextRead += new StringReadEventHandler(handleProcessOutput);
                io.StderrTextRead += new StringReadEventHandler(handleProcessOutput);
                io.StartProcessOutputRead();

                process.WaitForExit();

                Thread.Sleep(100);

                process.Close();
                io.StopMonitoringProcessOutput();
            }

            Outputs.Add(command);
        }