Uruchomienie aplikacji w tle, informacje zwrotne

0

Cześć. Udało mi się uruchomić program, czyli zewnętrzny plik *.exe podając parametry bez tworzenia zewnętrznego okna.
Chciałbym dodać możliwość przechwytywania np. do memoEdit informacji zwrotnych bo takowe program plik_app.exe wysyła, czyli to co uzyskam, uruchamiając aplikację za pomocą konsoli CMD z Windowsa.

Uruchamiania tejże aplikacji:

ExecuteCmd exe = new ExecuteCmd();
exe.ExecuteCommandAsync("plik_app -i " + parametr");

Cała klasa

using System;
using System.Threading;

namespace ExecuteCommand
{
    public class ExecuteCmd
    {
        #region ExecuteCommand Sync and Async
        /// <summary>
        /// Executes a shell command synchronously.
        /// </summary>
        /// <param name="command">string command</param>
        /// <returns>string, as output of the command.</returns>
        public void ExecuteCommandSync(object command)
        {
            try
            {
                // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.
                System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
                // The following commands are needed to redirect the standard output. 
                //This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();

                // Get the output into a string
                string result = proc.StandardOutput.ReadToEnd();

                // Display the command output.
                Console.WriteLine(result);
            }
            catch (Exception objException)
            {
                // Log the exception
            }
        }

        /// <summary>
        /// Execute the command Asynchronously.
        /// </summary>
        /// <param name="command">string command.</param>
        public void ExecuteCommandAsync(string command)
        {
            try
            {
                //Asynchronously start the Thread to process the Execute command request.
                Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
                //Make the thread as background thread.
                objThread.IsBackground = true;
                //Set the Priority of the thread.
                objThread.Priority = ThreadPriority.AboveNormal;
                //Start the thread.
                objThread.Start(command);
            }
            catch (ThreadStartException objException)
            {
                // Log the exception
            }
            catch (ThreadAbortException objException)
            {
                // Log the exception
            }
            catch (Exception objException)
            {
                // Log the exception
            }
        }
        #endregion
    }
}

0

Popatrz co sam (??) napisałeś:

                // The following commands are needed to redirect the standard output. 
                //This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;

pozdrawiaMM

0

Ten kod nie jest mojego autorstwa, dlatego nie wiem jak "na żywo", co 2 sekundy odczytywać bufor zwrotny.
Pomożesz?

0

Coś mi nie wychodzi, próbuje na różne sposoby i tak aplikacja blokuje mi się dopóki w CMD nie wykona się zadana w atrybutach praca.

System.Diagnostics.ProcessStartInfo ProcessInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
            ProcessInfo.UseShellExecute = false;
            ProcessInfo.CreateNoWindow = true;

            ProcessInfo.RedirectStandardOutput = true;
            using (Process process = Process.Start(ProcessInfo))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Console.Write(result);
                }
            }

1 użytkowników online, w tym zalogowanych: 0, gości: 1