0

I wrote a C# NET application (Console app that is run as a Service) that manages a Java process (Minecraft Game Server), and our Web Panel software manages my application. When the Java process stops, my application stops itself, and when my application stops, it stops the Java process.

The issue I am running into is that I deployed the software to all of our machines after extensive bug testing, but there seems to be a bug we missed where it is NOT shutting down the Java process sometimes. This bug is horrible as the Web Software tries to start my application, my application tries to start the Java process, but fails due to it being unable to IP bind (since the old process stayed open) and we wind up with 15-30 bugged Java processes running.

  • I am using CurrentDomain_UnhandledException to catch my application's crashes and call TerminateProcess().
  • I am using CtrlTypes.CTRL_C_EVENT and CtrlTypes.CTRL_CLOSE_EVENT to detect my application being closed which also calls the TerminateProcess() function.

I have tried both of the following methods...

static void TerminateProcess()
{
    log.LogMessage("Minecraft Process Shutdown.");
    SendProcessCmd("stop");
}

and

    static void TerminateProcess()
    {
        log.LogMessage("Minecraft Process Shutdown.");
        minecraftProcess.Kill();
    }

However, I seem to be missing another way that my C# application is being shut down, because both ways seem to leave a Java process running every once in a while that I can't reproduce locally.

2
  • You mean log.LogMessage("Minecraft Process Shutdown."); isn't called? Maybe there is a problem in the service wrapper? Instead, modify your application to be both a service and console Commented Aug 2, 2011 at 8:39
  • It always seems to be called, but doesn't always seem to stop the process. Can you explain what you mean by both a service and a console? Commented Aug 2, 2011 at 8:53

1 Answer 1

1

Well, you did not state any question, I’m going to guess you wanted to ask for other ways a process can get shut down, so that you can intercept it and ensure the Java process termination. Simply said: That is impossible (in full generality).

Just look at your own code: You are doing exactly the same thing to Minecraft: Calling TerminateProcess causes the target process to terminate immediately, without any chance to clean up. Should anyone call TerminateProcess on you (e.g. a user killing the process from Task Manager), your process just terminates immediately.

Also, some fatal exceptions are uncatchable, e.g. when your process dies on a stack overflow, you are not told, just terminated.

I guess you’d need to create another process, watching over your process… (Or rethink the whole architecture, creating and killing processes, especially with TerminateProcess, seems a bit rough to me.)

Sign up to request clarification or add additional context in comments.

3 Comments

After I launch the Java process, I keep mine open with a while (minecraftProcess.HasExited == false) which seems to work fine. Best I can tell mine always closes when that returns true. Is there a cleaner way to shutdown the Java process rather than using Kill() on it?
"Stop" is the command used to shut the process down gracefully. I am assuming the best way to do this might be to issue "Stop", then sleep for X seconds, check if it is still running, then kill it if so. Would that be the best approach?
There is no single universal method to “ask” another process to terminate cleanly. If “Stop” is the method in your specific case, then yes, by all means, try it, and only if it does not work, use Kill. (Also note that busy-waiting using while(something); is not a nice programming style. Use process.WaitForExit(timeout).)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.