-1

I would like to execute a function when the running application terminated via normal close way (right top X) or un expected error happened and software terminated.

How can i do this at c# 4.5 WPF application

Thank you

leppie
  • 115,091
  • 17
  • 196
  • 297
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

2 Answers2

1

In your App.xaml.cs -

  • Override OnStartUp method and hook UnhandledException event of Current AppDomain, it will get called whenever application was about to close because of some unhandled exception.
  • Override OnExit method for normal close of application.
  • Create CleanUp method and call the method from above two methods.

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        AppDomain.CurrentDomain.UnhandledException += new
           UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    }
    
    private void CleanUp()
    {
        // Your CleanUp code goes here.
    }
    
    protected override void OnExit(ExitEventArgs e)
    {
        CleanUp();
        base.OnExit(e);
    }
    
    void CurrentDomain_UnhandledException(object sender,
                                          UnhandledExceptionEventArgs e)
    {
        CleanUp();
    }
    
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • this is WPF application so it does not recognize OnStartup and OnExit – Furkan Gözükara Mar 02 '13 at 13:23
  • It does, you need to place this code in your `App.xaml.cs` file. Have you tried it? – Rohit Vats Mar 02 '13 at 13:25
  • but i am not able to access variables at main windows thread so this seems usless :( – Furkan Gözükara Mar 02 '13 at 13:28
  • Which variables you need to access? – Rohit Vats Mar 02 '13 at 13:28
  • i have public TextWriter and need to flush it when closing or un expected error handled. – Furkan Gözükara Mar 02 '13 at 13:34
  • 2
    please note that you asked the question "how to execute function when..". Both @RV1987 and I have showed you how you can execute a function (a method) when those conditions are met. The new problem you are facing is related to basic OOP and it implies general knowledge about encapsulation and addressability between various parts of your application. So it's not "useless", it's only midway of what you _actually_ wanted to ask... – Eduard Dumitru Mar 02 '13 at 13:47
0

You could handle the Exit event of the app's Application subclass main instance and the UnhandledException event of the current AppDomain instance like so:

public partial class App : Application {

    public App() {
        this.Exit += App_Exit;
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
        MessageBox.Show("Exception " + e.ExceptionObject.GetType().Name);
    }

    void App_Exit(object sender, ExitEventArgs e) {
        MessageBox.Show("Bye bye");
    }   

}

Please note that given the following (simulated by clicking some buttons) scenarios for unhandled exceptions:

enter image description here

which handle their respective click events like so:

    private void buttonThrowNice_Click(object sender, RoutedEventArgs e) {
        throw new Exception("test");
    }

    private void buttonStackOverflow_Click(object sender, RoutedEventArgs e) {
        this.buttonStackOverflow_Click(sender, e);
    }

    private void buttonFailFast_Click(object sender, RoutedEventArgs e) {
        Environment.FailFast("my fail fast");
    }

    private void buttonOutOfMemory_Click(object sender, RoutedEventArgs e) {
        decimal[,,,,,] gargantuan = new decimal[int.MaxValue,int.MaxValue,int.MaxValue,int.MaxValue, int.MaxValue, int.MaxValue];
        Debug.WriteLine("Making sure the compiler doesn't optimize anything: " + gargantuan.ToString());
    }

The UnhandledException event of the AppDomain class only handles:

  • regular exceptions
  • the OutOfMemoryException

whereas the:

  • failfast
  • and the StackOverflow exception

are not caught.

Eduard Dumitru
  • 3,242
  • 17
  • 31