Esempio n. 1
0
        /// <summary>
        /// Starts a process with settings for performing a headless, automated task.
        /// </summary>
        /// <param name="process">The process object that will perform the task.</param>
        /// <param name="onOutput">An action that will handle standard output and errors returned by the process.</param>
        /// <param name="synchronizeOutputEvents">
        /// If true, the invocations of the onOutput action will be serialized by wrapping the action in a lock.
        /// The output and error events are handled by different threads.
        /// To handle synchronization yourself, set this to false.</param>
        /// <returns></returns>
        public static bool StartAutomated(this Process process, Action<ProcessOutput> onOutput = null, bool synchronizeOutputEvents = true)
        {
            var info = process.StartInfo;
            info.UseShellExecute = false;
            info.CreateNoWindow = true;
            info.ErrorDialog = false;

            if (onOutput == null)
                return process.Start();

            if (synchronizeOutputEvents)
            {
                var mutex = new Object();
                var wrapped = onOutput;
                onOutput = o => { lock (mutex) wrapped(o); };
            }

            info.RedirectStandardOutput = true;
            info.RedirectStandardError = true;
            process.OutputDataReceived += (s, e) => { if (e.Data != null) onOutput(new ProcessOutput(e.Data, false)); };
            process.ErrorDataReceived += (s, e) => { if (e.Data != null) onOutput(new ProcessOutput(e.Data, true)); };

            var started = process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            return started;
        }
 public static void StartSilent(this Process p, LoggerForm aApp)
 {
     logger = aApp;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.RedirectStandardError = true;
     p.OutputDataReceived += P_OutputDataReceived;
     p.ErrorDataReceived += P_OutputDataReceived;
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.CreateNoWindow = true;
     p.EnableRaisingEvents = true;
     p.Start();
     p.BeginOutputReadLine();
     p.BeginErrorReadLine();
     p.WaitForExit();
 }
Esempio n. 3
0
        public static void Query(
            this Process proc,
            string command,
            string arguments,
            bool visible,
            string workingDir,
			Action<string,bool> onRecievedLine)
        {
            if (Environment.OSVersion.Platform != PlatformID.Unix &&
                Environment.OSVersion.Platform != PlatformID.MacOSX)
            {
                arguments = "/c " +
                    "^\"" + batchEscape(command) + "^\" " +
                    batchEscape(arguments);
                command = "cmd.exe";
            }

            var endOfOutput = false;
            var endOfError = false;
            prepareProcess(proc, command, arguments, visible, workingDir);
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.ErrorDataReceived += (s, data) => {
                if (data.Data == null)
                    endOfError = true;
                else
                    onRecievedLine("Error: " + data.Data, true);
            };
            proc.OutputDataReceived += (s, data) => {
                    if (data.Data == null)
                        endOfOutput = true;
                    else
                        onRecievedLine(data.Data, false);
                };

            if (proc.Start())
            {
                proc.BeginOutputReadLine();
                proc.BeginErrorReadLine();
                while (!endOfError || !endOfOutput)
                    System.Threading.Thread.Sleep(10);
            }
        }
        public static void Start(
            this Process process, 
            string command, 
            string arguments, 
            bool runInASeparateConsoleWindow,
            bool requireAdministratorPriviledges)
        {
            var isRunningOnMono = Thread.CurrentThread.IsRunningOnMono();

            var runInTheSameConsoleWindow = !runInASeparateConsoleWindow;

            process.StartInfo = new ProcessStartInfo
            {
                UseShellExecute = runInASeparateConsoleWindow,

                CreateNoWindow = runInTheSameConsoleWindow,
                RedirectStandardOutput = runInTheSameConsoleWindow,
                RedirectStandardError = runInTheSameConsoleWindow,

                Arguments =
                    isRunningOnMono
                        ? String.Format("{0} {1}", command, arguments)
                        : arguments,
                Verb = requireAdministratorPriviledges ? "runas" : null,
                FileName = (isRunningOnMono ? "mono" : command)
            };

            if (runInTheSameConsoleWindow)
            {
                process.OutputDataReceived += (s, e) => Console.WriteLine("> " + e.Data);
                process.ErrorDataReceived += (s, e) => Console.WriteLine(">> " + e.Data);
            }

            process.Start();

            if (runInTheSameConsoleWindow)
            {
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
            }

            Log.Info("Process '{0}' started with arguments: {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
        }
        private static int query(this Process proc, string command, string arguments,
                                 bool visible, string workingDir,
                                 Action<bool, string> onRecievedLine,
                                 IEnumerable<KeyValuePair<string,string>> replacements) {
            string tempFile = null;
			arguments = replaceArgumentPlaceholders(arguments, replacements);
            if (Environment.OSVersion.Platform != PlatformID.Unix &&
                Environment.OSVersion.Platform != PlatformID.MacOSX)
            {
                if (Path.GetExtension(command).ToLower() != ".exe") {
                    arguments = getBatchArguments(command, arguments, ref tempFile);
                    command = "cmd.exe";
                }
            }
			
            prepareProcess(proc, command, arguments, visible, workingDir);
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;

            DataReceivedEventHandler onOutputLine = 
                (s, data) => {
                    if (data.Data != null)
                        onRecievedLine(false, data.Data);
                };
            DataReceivedEventHandler onErrorLine = 
                (s, data) => {
                    if (data.Data != null)
                        onRecievedLine(true, data.Data);
                };

			proc.OutputDataReceived += onOutputLine;
            proc.ErrorDataReceived += onErrorLine;
            if (proc.Start())
            {
                proc.BeginOutputReadLine();
                proc.BeginErrorReadLine();
                proc.WaitForExit();
            }
            proc.OutputDataReceived -= onOutputLine;
            proc.ErrorDataReceived -= onErrorLine;
            
            if (tempFile != null && File.Exists(tempFile))
                File.Delete(tempFile);
            return proc.ExitCode;
        }
        private static int query(this Process proc, string command, string arguments,
                                 bool visible, string workingDir,
                                 Action<bool, string> onRecievedLine,
                                 KeyValuePair<string,string>[] replacements,
                                 Action<string> preparedArguments)
        {
            try {
                string tempFile = null;
                if (handleOiLnk(ref command, ref arguments, workingDir, onRecievedLine, replacements))
                    return 0;
                arguments = replaceArgumentPlaceholders(arguments, replacements);
                if (!prepareInterpreter(ref command, ref arguments)) {
                    if (Environment.OSVersion.Platform != PlatformID.Unix &&
                        Environment.OSVersion.Platform != PlatformID.MacOSX)
                    {
                        if (Path.GetExtension(command).ToLower() != ".exe") {
                            arguments = getBatchArguments(command, arguments, ref tempFile);
                            command = "cmd.exe";
                        }
                    }
                }

                prepareProcess(proc, command, arguments, visible, workingDir);
                preparedArguments(proc.StartInfo.Arguments);
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;

                DataReceivedEventHandler onOutputLine =
                    (s, data) => {
                        if (data.Data != null)
                            onRecievedLine(false, data.Data);
                    };
                DataReceivedEventHandler onErrorLine =
                    (s, data) => {
                        if (data.Data != null)
                            onRecievedLine(true, data.Data);
                    };

                proc.OutputDataReceived += onOutputLine;
                proc.ErrorDataReceived += onErrorLine;
                Logger.Write("Running: {0} {1}", proc.StartInfo.FileName, proc.StartInfo.Arguments);
                if (proc.Start())
                {
                    proc.BeginOutputReadLine();
                    proc.BeginErrorReadLine();
                    proc.WaitForExit();
                }
                proc.OutputDataReceived -= onOutputLine;
                proc.ErrorDataReceived -= onErrorLine;

                if (tempFile != null && File.Exists(tempFile))
                    File.Delete(tempFile);
                return proc.ExitCode;
            } catch (Exception ex) {
                _dispatcher("Failed to run " + command + ". Is the file/command not executable or are you missing a configured interpreter?");
                Logger.Write(ex);
                return 0;
            }
        }