当前位置:编程学习 > C#/ASP.NET >>

监控Console程序

ConsoleA.exe 不定时的输出,同时该程序有可能发生异常导致程序崩溃。

如何用其他程序监控ConsoleA.exe,获取它的输出内容,并且不受ConsoleA.exe异常的影响?也就是监控的程序和被监控的程序(两个程序)同时运行。


我尝试过在自定义程序中,用Process类启动ConsoleA.exe,并开启输出重定向,可以获取到ConsoleA.exe的输出。但这种方式在ConsoleA.exe发生异常崩溃的时候,自定义的程序也会崩溃掉。


StreamReader sr;
ProcessStartInfo s = new ProcessStartInfo();
s.RedirectStandardOutput = true;
s.UseShellExecute = false;
s.FileName = @"C:\ConsoleThrowException.exe";
Process p = Process.Start(s);
sr = p.StandardOutput;
while (!sr.EndOfStream)
{
   string str = sr.ReadLine();
   Console.WriteLine(str);
}
p.WaitForExit();
p.Close();
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,