使用 Process 类检查控制台应用程序是否仍在运行
我正在制作一个应用程序,它将监视另一个进程的状态,并在它停止响应、退出或抛出错误时重新启动它。 但是,我无法可靠地检查进程(作为 C++ 控制台窗口)是否已停止响应。
我的代码如下所示:
public void monitorserver()
{
while (true)
{
server.StartInfo = new ProcessStartInfo(textbox_srcdsexe.Text, startstring);
server.Start();
log("server started");
log("Monitor started.");
while (server.Responding)
{
if (server.HasExited)
{
log("server exitted, Restarting.");
break;
}
log("server is running: " + server.Responding.ToString());
Thread.Sleep(1000);
}
log("Server stopped responding, terminating..");
try
{ server.Kill(); }
catch (Exception) { }
}
}
我正在监视的应用程序是 Valve 的 Source 专用服务器,运行 Garry 的 Mod,并且我过度强调物理引擎来模拟它停止响应。 然而,这永远不会触发进程类将其识别为“停止响应”。
我知道有一些方法可以使用自己的协议直接查询源服务器,但我想保持它的简单和通用(这样我将来就可以将它用于不同的应用程序)。
任何帮助表示赞赏
I'm making an application that will monitor the state of another process and restart it when it stops responding, exits, or throws an error.
However, I'm having trouble to make it reliably check if the process (Being a C++ Console window) has stopped responding.
My code looks like this:
public void monitorserver()
{
while (true)
{
server.StartInfo = new ProcessStartInfo(textbox_srcdsexe.Text, startstring);
server.Start();
log("server started");
log("Monitor started.");
while (server.Responding)
{
if (server.HasExited)
{
log("server exitted, Restarting.");
break;
}
log("server is running: " + server.Responding.ToString());
Thread.Sleep(1000);
}
log("Server stopped responding, terminating..");
try
{ server.Kill(); }
catch (Exception) { }
}
}
The application I'm monitoring is Valve's Source Dedicated Server, running Garry's Mod, and I'm over stressing the physics engine to simulate it stopping responding.
However, this never triggers the process class recognizing it as 'stopped responding'.
I know there are ways to directly query the source server using their own protocol, but i'd like to keep it simple and universal (So that i can maybe use it for different applications in the future).
Any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
响应
属性 指示进程是否正在运行未挂起的Windows 消息循环。正如文档所述,
正如您所尝试的那样,不可能检查任意进程是否正在执行任意操作。
The
Responding
property indicates whether the process is running a Windows message loop which isn't hung.As the documentation states,
It is not possible to check whether an arbitrary process is doing an arbitrary thing, as you're trying to.