使用 Process 类检查控制台应用程序是否仍在运行

发布于 2024-10-10 16:02:13 字数 1076 浏览 1 评论 0原文

我正在制作一个应用程序,它将监视另一个进程的状态,并在它停止响应、退出或抛出错误时重新启动它。 但是,我无法可靠地检查进程(作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-10-17 16:02:13

响应属性 指示进程是否正在运行未挂起的Windows 消息循环
正如文档所述,

如果进程没有 MainWindowHandle,则此属性返回 true。

正如您所尝试的那样,不可能检查任意进程是否正在执行任意操作。

The Responding property indicates whether the process is running a Windows message loop which isn't hung.
As the documentation states,

If the process does not have a MainWindowHandle, this property returns true.

It is not possible to check whether an arbitrary process is doing an arbitrary thing, as you're trying to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文