2
AppDomain.CurrentDomain.UnhandledException += 
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Forms.Application.ThreadException += 
    new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

was the code i snipped off a similar question, but it fails to solve my case. What i'm trying to do is catch a crash and dispose of my application correctly so i don't leave behind memory dumps/traces/systray icons

Jehof
  • 34,674
  • 10
  • 123
  • 155
pl0xtic
  • 33
  • 1
  • 3
  • 1
    What exception is not get caught by `AppDomain Exception handler`? Can you show some code? – Rohit Vats Apr 14 '13 at 13:54
  • It's when i close it in the Visual Studio debugger, it'll leave behind systray icons and other undisposed items that i'm pretty sure can't be recovered later on... How would I force it to shut down properly, I'm afraid it will happen from another cause also and can cause some end-user problems since it's not in the debugger anymore. – pl0xtic Apr 14 '13 at 14:07
  • 1
    "t's when i close it in the Visual Studio debugger" is it an exception or not? If you just stop debugging a process, of course that the cleanup won't be executed as you effectively abort the process. – Zdeslav Vojkovic Apr 14 '13 at 14:10
  • what version of .NET framework are you using? – Zdeslav Vojkovic Apr 14 '13 at 14:14

1 Answers1

0

If you terminate the process using debugger or task manager, there's no way to handle this situation -- be it a .NET application or a native one.

Handling OS exceptions in CLR may be possible, but it is not advised. See Is it possible to catch an access violation exception in .NET? for more details.

In almost all cases, your process should not be terminated in these ways, and even if it does, proper disposing is unnecessary, as OS will properly close almost all handles: files, connections etc. Terminated applications leaving tray icons behind is a 20-year old bug in Explorer and you can't do anything about it. Let's just hope Microsoft will fix it one day.

Community
  • 1
  • 1
Athari
  • 33,702
  • 16
  • 105
  • 146