简体   繁体   中英

C# NET Application not shutting down Java Process

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.

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 (eg a user killing the process from Task Manager), your process just terminates immediately.

Also, some fatal exceptions are uncatchable, eg 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM