【发布时间】:2013-12-30 07:21:39
【问题描述】:
我正在编写一个函数来创建一个进程并运行它。
这是原来的功能:
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);
这个可以吗?
【问题讨论】:
标签: c# function process delegates output-redirect