C# 将void(对象、DataReceivedEventArgs)传递给函数

C# 将void(对象、DataReceivedEventArgs)传递给函数,c#,function,process,delegates,output-redirect,C#,Function,Process,Delegates,Output Redirect,我正在编写一个函数来创建一个进程并运行它 以下是原始函数: public static void LaunchApplication(string runCommand, string commandArguments, string workingDir, string informMessage) { try { Process applicationProcess = new Process(); applicationProcess.Sta

我正在编写一个函数来创建一个进程并运行它

以下是原始函数:

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;
    }
}
我想将函数的签名更改为:

public static void LaunchApplication(string runCommand, string commandArguments, string workingDir, string informMessage, Func<object, DataReceivedEventArgs> myMethodName)
applicationProcess.OutputDataReceived += new DataReceivedEventHandler(myMethodName);
可以这样做吗?

试试这个:

public static void LaunchApplication(string runCommand, string commandArguments, string workingDir, string informMessage, Action<string> redirectOutput, Action<string> redirectError)
{
    try
    {
        Process applicationProcess = new Process();
        applicationProcess.StartInfo.FileName = runCommand;
        applicationProcess.StartInfo.WorkingDirectory = workingDir;
        applicationProcess.StartInfo.Arguments = commandArguments;
        applicationProcess.StartInfo.UseShellExecute = false;
        applicationProcess.EnableRaisingEvents = true;
        applicationProcess.StartInfo.CreateNoWindow = true;

        if (redirectOutput != null)
            applicationProcess.OutputDataReceived += (s, data) => redirectOutput(data.Data);

        if (redirectError != null)
            applicationProcess.ErrorDataReceived += (sErr, errData) => redirectError(errData.Data);

        GeneralUtils.WriteHeadlineToConsole(informMessage);
        applicationProcess.Start();
        if (redirectOutput != null)
            applicationProcess.BeginOutputReadLine();
        if (redirectError != null)
            applicationProcess.BeginErrorReadLine();
        applicationProcess.WaitForExit();
        GeneralUtils.WriteHeadlineToConsole("Finished " + informMessage);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return;
    }
}
PS: 下面的代码太糟糕了
AccessDeniedException
可能被抛出,CPU快疯了

while (Process.GetProcesses().Any(runningProcess => runningProcess.Id == applicationProcess.Id)) { }

使用
applicationProcess.WaitForExit()取而代之

在实现所有更改后,我测试了代码,但现在我得到了:
StandardOut尚未重定向或进程尚未启动。
尝试调用
applicationProcess.BeginOutputReadLine()时出现异常。我用更改更新了您的代码。有什么想法吗?我看不到下面这行
applicationProcess.StartInfo.RedirectStandardOutput=true。我想这是需要的。我的错,忘了。再次非常感谢!!
while (Process.GetProcesses().Any(runningProcess => runningProcess.Id == applicationProcess.Id)) { }