我有一个应用程序,它在命令窗口中调用另一个进程,该进程具有输出到控制台窗口的更新统计信息。我认为这是一个相当简单的操作,但我似乎不能让它工作。我是不是遗漏了什么?
string assemblyLocation = Assembly.GetExecutingAssembly().Location;
Process process = new Process
{
ProcessStart =
{
RedirectStandardOutput = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = arg,
FileName = assemblyLocation.Substring(0, assemblyLocation.LastIndexOf("\\")) + "\\ffmpeg.exe",
CreateNoWindow = true
}
};
process.Start();
Console.WriteLine(process.StandardOutput.ReadToEnd());
process.WaitForExit();理想情况下,我希望的是,当我点击的进程中的输出发生变化,或者数据进入阅读器时,我可以从中获取事件。
任何帮助都是很好的,我觉得这是一个新手问题,但似乎遗漏了一些东西。
发布于 2009-07-17 22:37:57
我以前也经历过这种情况。有时,您调用的进程输出到控制台的方式与这种输出重定向不兼容。在这种情况下,我很幸运能够修改外部进程来解决这个问题。
您可以尝试在另一个输出到控制台的进程上运行代码,并查看它是否正常工作。它现在正在读给我听。
编辑:
我取了一个我用来做这件事的代码块。这是在一个WPF应用程序中,它将进程输出重定向到窗口。注意事件绑定。因为这是WPF,所以我必须调用我的调用来写出数据。既然你不担心阻塞,你应该能够简单地将其替换为:
Console.WriteLine(e.Data);希望它能帮上忙!
private static void LaunchProcess()
{
Process build = new Process();
build.StartInfo.WorkingDirectory = @"dir";
build.StartInfo.Arguments = "";
build.StartInfo.FileName = "my.exe";
build.StartInfo.UseShellExecute = false;
build.StartInfo.RedirectStandardOutput = true;
build.StartInfo.RedirectStandardError = true;
build.StartInfo.CreateNoWindow = true;
build.ErrorDataReceived += build_ErrorDataReceived;
build.OutputDataReceived += build_ErrorDataReceived;
build.EnableRaisingEvents = true;
build.Start();
build.BeginOutputReadLine();
build.BeginErrorReadLine();
build.WaitForExit();
}
// write out info to the display window
static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
string strMessage = e.Data;
if (richTextBox != null && !String.Empty(strMessage))
{
App.Instance.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate()
{
Paragraph para = new Paragraph(new Run(strMessage));
para.Margin = new Thickness(0);
para.Background = brushErrorBrush;
box.Document.Blocks.Add(para);
});
}
} 发布于 2009-07-17 22:50:15
我不确定您到底遇到了什么问题,但是如果您希望在输出生成后立即采取行动,请尝试挂钩到进程的OutputDataReceived事件。您可以指定处理程序从进程异步接收输出。我已经成功地使用了这种方法。
Process p = new Process();
ProcessStartInfo info = p.info;
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
p.OutputDataReceived += p_OutputDataReceived;
p.ErrorDataReceived += p_ErrorDataReceived;
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();。。
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("Received from standard out: " + e.Data);
}
void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("Received from standard error: " + e.Data);
}有关详细信息,请参阅OutputDataReceived事件关闭过程。
发布于 2010-08-29 06:46:18
使用lambda表达式等:
var info = new ProcessStartInfo(path)
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
Verb = "runas",
};
var process = new Process
{
EnableRaisingEvents = true,
StartInfo = info
};
Action<object, DataReceivedEventArgs> actionWrite = (sender, e) =>
{
Console.WriteLine(e.Data);
};
process.ErrorDataReceived += (sender, e) => actionWrite(sender, e);
process.OutputDataReceived += (sender, e) => actionWrite(sender, e);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();https://stackoverflow.com/questions/1145969
复制相似问题