I'm writing a function which creates a Process and runs it.
Here is the original function:
public static void LaunchApplication(string runCommand, string commandArguments, string workingDir, string informMessage)
{
try
{
Process applicationProcess = new Process();
applicationProcess.StartInfo.FileName = runCommand;
applicationProcess.StartInfo.WorkingDirectory = workingDir;
applicationProcess.StartInfo.Arguments = commandArguments;
applicationProcess.StartInfo.UseShellExecute = false;
WriteHeadlineToConsole(informMessage);
applicationProcess.Start();
while (Process.GetProcesses().Any(runningProcess => runningProcess.Id == applicationProcess.Id)) { }
WriteHeadlineToConsole("Finished " + informMessage);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
}
I would like to change the functions' signature to something like:
public static void LaunchApplication(string runCommand, string commandArguments, string workingDir, string informMessage, Func<object, DataReceivedEventArgs> myMethodName)
And then inside the function, redirect the output, something like:
applicationProcess.OutputDataReceived += new DataReceivedEventHandler(myMethodName);
Can this be done?
Try this:
Usage:
PS: The following code is awful!
AccessDeniedException
might be thrown, CPU is going crazy!Use
applicationProcess.WaitForExit();
instead!