在 C# 中重用 Process 对象和 IO 重定向
我使用 Process 类的数据就绪事件从正在运行的进程的标准输出和标准错误中获取信息。
它在第一次运行时效果很好,但是在调用 Stop() 然后 Start() 强制重新启动应用程序后,我不再收到数据。我尝试过 CancelErrorRead() 但没有运气。
我正在考虑每次需要重新运行应用程序时重新实例化该对象,但需要这样做似乎很愚蠢。
关于如何重新使用 Process 对象来重新启动已停止的进程有什么建议吗?
相关代码块:
构造函数:
ProcessStartInfo objStartInfo = new ProcessStartInfo();
objStartInfo.CreateNoWindow = true;
objStartInfo.RedirectStandardInput = true;
objStartInfo.RedirectStandardOutput = true;
objStartInfo.RedirectStandardError = true;
objStartInfo.UseShellExecute = false;
objClient = new Process();
objClient.StartInfo = objStartInfo;
objClient.EnableRaisingEvents = true;
objClient.OutputDataReceived += new DataReceivedEventHandler(read);
objClient.ErrorDataReceived += new DataReceivedEventHandler(error);
开始:
objClient.StartInfo.FileName = strAppPath;
objClient.StartInfo.Arguments = strArgs;
start();
objClient.BeginErrorReadLine();
objClient.BeginOutputReadLine();
停止:
objClient.Close();
objClient.CancelErrorRead();
objClient.CancelOutputRead();
I'm using the data ready events of the Process class to get information from the standard output and standard error of a running process.
It works great on the first run, but after calling Stop() then Start() to force a restart of the application, I no longer recieve data. I've tried CancelErrorRead() but no luck there.
I'm considering just re-instantiating the object every time I need to re-run the app, but it seems silly to need to do that.
Any advice on how to re-use a Process object to restart a stopped process?
Relevant code chunks:
Constructor:
ProcessStartInfo objStartInfo = new ProcessStartInfo();
objStartInfo.CreateNoWindow = true;
objStartInfo.RedirectStandardInput = true;
objStartInfo.RedirectStandardOutput = true;
objStartInfo.RedirectStandardError = true;
objStartInfo.UseShellExecute = false;
objClient = new Process();
objClient.StartInfo = objStartInfo;
objClient.EnableRaisingEvents = true;
objClient.OutputDataReceived += new DataReceivedEventHandler(read);
objClient.ErrorDataReceived += new DataReceivedEventHandler(error);
Start:
objClient.StartInfo.FileName = strAppPath;
objClient.StartInfo.Arguments = strArgs;
start();
objClient.BeginErrorReadLine();
objClient.BeginOutputReadLine();
Stop:
objClient.Close();
objClient.CancelErrorRead();
objClient.CancelOutputRead();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用
Start()
(或使用Process
中的静态方法之一)之前,您的Process
对象不会与进程关联。停止/关闭的进程在功能上与根本没有进程相同。鉴于此,与在 Windows 上创建进程的(相对巨大的)成本相比,很难相信创建新的 Process 对象会产生任何开销。只需根据需要创建新的 Process 对象即可。Your
Process
object is not associated with a process until you callStart()
(or use one of the static methods offProcess
). A stopped/closed process is functionally the same as no process at all. Given that, it's hard to believe there's any overhead to creating a new Process object, when compared to the (relatively enormous) cost of creating processes on Windows. Just create new Process objects as needed.根据 msdn,您应该调用
BeginOutputReadLine
和BeginErrorReadLine
来启用使用事件从 StandardOutput 或 StandardError 进行异步读取。看一下评论部分
BeginOutputReadLine
According to msdn you should call
BeginOutputReadLine
andBeginErrorReadLine
to enable asynchronous reads from StandardOutput or StandardError using events.Have a look at the remarks section on
BeginOutputReadLine