3

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Is this a a) Winforms app, b) WPF app, c) console app, d) ASP.NET web app? Exception handling (on a "global" scope) is quite different between those types - knowing which one your app is would be crucial. – marc_s Jul 31 '11 at 07:48
  • 4
    Re: your edit - The general advice is: If you can't recover from an exception, let it bubble up, or log and re-throw it. Don't swallow exceptions, and don't write catch blocks that don't actually handle the exceptional case. – Merlyn Morgan-Graham Jul 31 '11 at 07:49
  • Console App that is being run as a service, sorry. –  Jul 31 '11 at 07:54
  • Merlyn, if you want to answer I will accept that, thank you! How do you rethrow an exception from a catch block? –  Jul 31 '11 at 08:02
  • Just a single `thow` would rethrow. – Emond Jul 31 '11 at 08:07
  • There are some exceptions that will end your .net process immediately without calling your DomainUnhandledException handler, mainly ones generated by calling into unmanaged code. The event log should have information on any crashes so you can see what kind of exception was thrown. – BrandonAGr Jul 31 '11 at 09:10

2 Answers2

1

First of all you have to change the Console.WriteLine.. lines in you code to Debug.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 do

try
{
    ...
}
catch (Exception exception)
{
    Log(exceptiosn);//log it first or Debug.WriteLine...
#if DEBUG
    System.Diagnostics.Debugger.Break();//break at the debugger here.
#endif//DEBUG
    throw;
} 

After you cleaning up you code, now you can whenever DomainUnhandledException thrown, 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.

Community
  • 1
  • 1
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
0

Something to consider is whether you want the .NET application to be responsible for the spawned processes. If possible, they might be made responsible for shutting down when no longer receiving input. If the spawned processes are running on other machines I would try to do that anyway because network problems might interfere with sending a shutdown message from the .NET application to the Java processes. Each its own responsibilities.

That said, getting exception handling in the .NET application fixed (especially when you are missing some exceptions) is also important. Make each thread responsible for its exceptions and make sure you log them for easy debugging.

Emond
  • 50,210
  • 11
  • 84
  • 115