我有一个奇怪的问题,我似乎无法诊断。
我正在调用外部二进制文件,等待它完成,然后根据标准输出传回结果。我也想记录错误。
我写了这个,它在 Windows 7 中很有效
namespace MyApp
{
class MyClass
{
public static int TIMEOUT = 600000;
private StringBuilder netOutput = null;
private StringBuilder netError = null;
public ResultClass runProcess()
{
using (Process process = new Process())
{
process.StartInfo.FileName = ConfigurationManager.AppSettings["ExeLocation"];
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(ConfigurationManager.AppSettings["ExeLocation"]);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(NetOutputDataHandler);
netOutput = new StringBuilder();
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);
netError = new StringBuilder();
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
if (process.WaitForExit(TIMEOUT))
{
// Process completed handle result
//return my ResultClass object
}
else
{
//timed out throw exception
}
}
}
private void NetOutputDataHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
//this is being called in Windows 7 but never in Windows Server 2008
if (!String.IsNullOrEmpty(outLine.Data))
{
netOutput.Append(outLine.Data);
}
}
private void NetErrorDataHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
//this is being called in Windows 7 but never in Windows Server 2008
if (!String.IsNullOrEmpty(outLine.Data))
{
netError.Append(outLine.Data);
}
}
}
}
所以我将它安装在 Windows Server 2008 机器上,并且永远不会调用 NetOutputDataHandler 和 NetErrorDataHandler 处理程序。
该应用程序是为 .NET v4 编译的。
任何想法我做错了什么?