从我的应用程序生成的小型长时间运行的控制台应用程序获取一些控制台标准输出时遇到问题。
我的应用程序启动控制台应用程序,并且控制台应用程序将在我的应用程序生命周期内(或直到显式终止)一直监听端口。
当控制台应用程序启动时,它会输出它正在侦听的端口号,我需要异步获取该端口号并在应用程序的其他地方使用它。问题是,我获取输出数据的事件处理程序永远不会被调用。我确信一定有一些我忘记做的微不足道的事情。
ProcessStartInfo si = new ProcessStartInfo();
si.FileName = theFileName;
si.Arguments = theargs;
si.UseShellExecute = false;
si.RedirectStandardOutput = true;
si.CreateNoWindow = true;
_process = Process.Start(si);
_process.OutputDataReceived += (sender, args) =>
{
//Parse the args.Data string for the port number.
};
_process.BeginOutputReadLine(); // My handler never gets called.
// I don't want to call _process.WaitForExit() here as this is a long running process.