Search code examples
c#functioncalling-convention

Newbie, howto to start this function


please sorry for this stupid question, i'm newbie to c# and my Vb is untouched by many years..

Based on this article: Process Start

Here the code:

 public static int Run(Action<string> output, TextReader input, string exe, params string[] args)
  {
     if (String.IsNullOrEmpty(exe))
        throw new FileNotFoundException();
     if (output == null)
        throw new ArgumentNullException("output");

     ProcessStartInfo psi = new ProcessStartInfo();
     psi.UseShellExecute = false;
     psi.RedirectStandardError = true;
     psi.RedirectStandardOutput = true;
     psi.RedirectStandardInput = true;
     psi.WindowStyle = ProcessWindowStyle.Hidden;
     psi.CreateNoWindow = true;
     psi.ErrorDialog = false;
     psi.WorkingDirectory = Environment.CurrentDirectory;
     psi.FileName = FindExePath(exe); //see http://csharptest.net/?p=526
     psi.Arguments = EscapeArguments(args); // see http://csharptest.net/?p=529

     using (Process process = Process.Start(psi))
     using (ManualResetEvent mreOut = new ManualResetEvent(false),
     mreErr = new ManualResetEvent(false))
     {
        process.OutputDataReceived += (o, e) => { if (e.Data == null) mreOut.Set(); else output(e.Data); };
        process.BeginOutputReadLine();
        process.ErrorDataReceived += (o, e) => { if (e.Data == null) mreErr.Set(); else output(e.Data); };
        process.BeginErrorReadLine();

        string line;
        while (input != null && null != (line = input.ReadLine()))
           process.StandardInput.WriteLine(line);

        process.StandardInput.Close();
        process.WaitForExit();

        mreOut.WaitOne();
        mreErr.WaitOne();
        return process.ExitCode;
     }
  }

... how can i call the function?

I modified the function with this:

public static int Run(Action<string> output, TextReader input, string exe, string args)

...because i already know the exe path and i wanto to directly pass args as direct string, but i don't know how to use the output and input variables.

By the way i understand the functionality but how to call it? To clarify please help me to fill ? here:

Run(???, ???, "console.exe", " -someargs");

A code example will be really appreciated... and again sorry for my stupid question and for my bad english language too.

Regards


Solution

  • Assuming you are not interested in the output the exe produces and you do not want to input any data into the process you can call the function like this:

    Run((outMsg) => {}, null, "console.exe", " -someargs");
    

    Explanation

    The first parameter is a Action<string> which means it expects a function with one string argument. Every data that is received from the process on the standard output or standard error is passed to this function. In my example above i just inserted a lambda expression which accepts one argument and does nothing.

    The second parameter is a TextReader instance which seems to be optional and thus can be passed as null when not needed. If set the content of the TextReader is written to the standard input of the process.