Checking if a console application is still running using the Process class

Posted by Ced on Stack Overflow See other posts from Stack Overflow or by Ced
Published on 2011-01-02T19:48:34Z Indexed on 2011/01/02 19:53 UTC
Read the original article Hit count: 254

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET