views:

204

answers:

2

Is there any way to automatically remove a NotifyIcon in the event of a crash? (I know you can mouse-over to remove it)

I'm running windows xp.

+3  A: 

For C#, try handling the UnhandledException event from the AppDomain, so, in your Main() method add:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

And then add the following method:

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        // .... Remove Notification icon here
    }
Rob
+3  A: 

Unfortunately the answer is no - Rob's answer actually detects the crash and attempts to remove the icon in the crash handler which has its own set of issues (for instance it assumes that enough of the CLR is running at the time of the crash to execute the unhandled exception, that's not always the case).

The problem here is that the shell (which runs the Shell_NotifyIcon code) gets out of sync with your application.

Larry Osterman