Пример #1
0
        /// <summary>
        /// Runs a command with arguments.
        /// </summary>
        /// <param name="flashMulti">An instance of the <see cref="FlashMulti"/> class.</param>
        /// <param name="command">The command to run.</param>
        /// <param name="args">Arguments for the command.</param>
        /// <returns>The exit code returned by the command.</returns>
        public static int Run(FlashMulti flashMulti, string command, string args)
        {
            Debug.WriteLine("\n" + command + " " + args + "\n");
            flashMulti.AppendVerbose(command + " " + args + "\r\n");

            // Check if the file exists
            if (!File.Exists(command))
            {
                flashMulti.AppendVerbose(string.Format("{0} does not exist", command));
                return(-1);
            }

            Process myProcess = new Process();

            // Process process = new Process();
            myProcess.StartInfo.FileName               = command;
            myProcess.StartInfo.Arguments              = args;
            myProcess.StartInfo.UseShellExecute        = false;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.StartInfo.RedirectStandardError  = true;
            myProcess.StartInfo.CreateNoWindow         = true;

            // avrdude sends all output to stderr, so reverse the sync/async processing
            if (command.EndsWith("avrdude.exe"))
            {
                // Handle standard output asynchronously
                myProcess.OutputDataReceived += new DataReceivedEventHandler(flashMulti.OutputHandler);

                // Start process and handlers
                myProcess.Start();

                // Read the standard output asynchronously, handle it by line
                myProcess.BeginOutputReadLine();

                // Read the error output synchronously, handle it character-by-character
                while (!myProcess.StandardError.EndOfStream)
                {
                    var data = myProcess.StandardError.Read();
                    flashMulti.CharOutputHandler((char)data);
                }
            }
            else
            {
                // Hande error output asynchronously
                myProcess.ErrorDataReceived += new DataReceivedEventHandler(flashMulti.OutputHandler);

                // Start process and handlers
                myProcess.Start();

                // Read the error output asynchronously, handle it by line
                myProcess.BeginErrorReadLine();

                // Read the standard output synchronously, handle it character-by-character
                while (!myProcess.StandardOutput.EndOfStream)
                {
                    var data = myProcess.StandardOutput.Read();
                    flashMulti.CharOutputHandler((char)data);
                }
            }

            // Loop until the process finishes
            myProcess.WaitForExit();

            int returnCode = myProcess.ExitCode;

            myProcess.Dispose();

            // Return the exit code from the process
            return(returnCode);
        }