1

I am currently trying to write a windows form application (in C#) that can start and stop multiple Java processes (with parameters to run a specific jar file).

I have no problem starting each process; however I need to find a way to make the application close all of them when it exits, regardless of which way (being an unknown amount of java processes), that I run in an individual worker thread each to avoid tying up the main thread while the application is running (and catching the processes outputs).

I had a look at this one: Close another process when application is closing

but it does not seem to work for my purpose (it doesn't close the processes).

Community
  • 1
  • 1

2 Answers2

1

it does not seem to work for my purpose.. (it doesn't close the processes).

But what does it do? Does it close the Java window(s) at least? Do your Java applications even have windows?

In general,

  1. If possible (i.e. if you build the Java application yourself) you should set up a mechanism between your C# and Java application(s) to gracefully signal the Java application(s) to shut down (socket, etc.)
  2. Failing that, you may still be able to gracefully shut down your Java application(s), if they are graphical, by sending WM_CLOSE. This is what the Process.CloseMainWindow/Process.Close approach that you tried (and failed) does. If your Java applications are console applications, you can try closing its/their standard input and/or simulating ^C instead.
  3. Finally, when all else fails, use Process.Kill to terminate your Java child process(es) -- ungracefully. You may want your controlling process to first try 1. or 2. above, wait until either all child processes have exited or until a short period of time (e.g. 3s) has elapsed, and only then proceed with Process.Kill on whatever processes have not exited already.
Community
  • 1
  • 1
vladr
  • 65,483
  • 18
  • 129
  • 130
  • I'm running for the tests here just the minecraft_server.jar (minecraft.net/download) it didn't seem to do anything, I started the process with the window hidden (since it's a console anyways, and I'll be redirecting in/output to my application), and after closing the application I still had "java.exe" running. right now i'm not caring about it being gracefull, I am gonna implement that eventually by sending a command to the java process input (Ie "stop" for the minecraft server) – user1586792 Nov 18 '12 at 18:09
  • OK, then the `CloseMainWindow` will not work. Does sending the `stop` command to its standard input not work? (Are your pipes set up properly to begin with?) If not then just `Process.Kill` it for now, and troubleshoot your pipes and handle inheritance later. – vladr Nov 18 '12 at 18:23
  • After more debugging I got http://stackoverflow.com/questions/943681/close-another-process-when-application-is-closing working (and getting them in individual threads) however, it seems that when I let them open in their own window (not hiding it) it closes them as expected, but when I use: myProcessInfo.CreateNoWindow = true; to have them run hidden, then they won't stop. – user1586792 Nov 18 '12 at 18:58
0

procrss.kill The Kill method is an excellent way to cause a Process to meet a violent and swift end. The Kill method can throw some exceptions. But it often does not and usually will accomplish your desired objective—which is somewhere between cold-blooded murder and a swift and painless execution.

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70