There are already some pretty good threads on this topic on Stack Overflow, but there doesn’t really seem to be a concise answer on any of them. My C# console application (running as a Windows service) launches a Java process and manages it (starts/stops/restarts it), but my issue is that I will remote into machines, and see it has started about 20 Java processes sometimes.
This is obviously an issue with my application crashing at some point, and not shutting down the Java process it started. I have hooked “UnhandledExceptionEventHandler” in the AppDomain.CurrentDomain, and I call TerminateProcess() from it (shuts down the active Java process) but this issue is still occuring on occassion.
My application has the Main thread, a TCP Server Thread (which accepts async connections), and a UDP Server Thread. Is there anything else I should be hooking into on top of UnhandledException?
EDIT
It also just occured to me that I have a few Try/Catch blocks in my code that simply write to console, which I never see. Should I just remove these so these are caught by the UnhandledException or add a logger there instead?
First of all you have to change the
Console.WriteLine..lines in you code toDebug.WriteLine..if you don’t want to log, so the output of it will only be on debug.Second when any exception occurs if you don’t know how to handle it or fix it then rethrow it
catch { throw; }after logging. I personally doAfter you cleaning up you code, now you can whenever
DomainUnhandledExceptionthrown, you can restart your application. an example will be here, The idea is you start a new instance from your application and then terminate the first one. you also define a mutex so only one instance at time will be alive.