1

I need to automatically restart my WPF when it crashes. I tried a few things now but my app still crashes and doesn't catch the error. The second problem is I don't know why it crashes. Well I don't know the exception, VS doesn't tell me. Just vshost32.exe has stopped working. All I know is it happens if I load a lot of different files in a small amount of time in a MediaElement. (Maybe Overflow exception or something)

This is my code. It's in the App.xaml and the with "StartupUri" in the XAML removed.

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

        try
        {
            MainWindow m = new MainWindow();
            m.Show();
        }
        catch (Exception)
        {
            MessageBox.Show("test");
        }
    }

    static void MyHandler(object sender, UnhandledExceptionEventArgs args)
    {
        Exception e = (Exception)args.ExceptionObject;
        MessageBox.Show("MyHandler caught : " + e.Message);
    }

    private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        MessageBox.Show("test");
    }

EDIT// Found the exception in the errorlog of windows: roughly translated it's this:

Anwendung: newKnoblauch.exe Frameworkversion: v4.0.30319 Description: The process has stopped because of a unhandled exception. Exceptioninfo: Exceptioncode c0000005, Exceptionadresse 55542ECC

Name der fehlerhaften Anwendung: newKnoblauch.exe, Version: 1.0.0.0, Time: 0x4d62e0a6 Name des fehlerhaften Moduls: wmp.dll, Version: 12.0.7600.16667, Time: 0x4c7dd593 Exceptioncode: 0xc0000005 Erroroffset: 0x00182ecc ID des fehlerhaften Prozesses: 0xa98

martinyyyy
  • 1,652
  • 3
  • 21
  • 44
  • Check out the answer in this [thread](http://stackoverflow.com/questions/1225406/if-wpf-app-is-not-responding-then-auto-restart). – Bala R Feb 21 '11 at 22:03
  • doesn't work at all for me.. even with "throw new ArgumentNullException();" it doesnt restart – martinyyyy Feb 21 '11 at 22:38

2 Answers2

3

I'm not sure what it is you're doing to cause this crash, but since .Net 2, StackOverflowException can not be caught. Reference: C# catch a stack overflow exception

An OutOfMemoryException would also cause unpredictable behavior. These are the kinds of serious exceptions that you can't really plan for. Fix the bugs that are causing them and move on. :)

Community
  • 1
  • 1
Robert Jeppesen
  • 7,837
  • 3
  • 35
  • 50
3

In order to implement auto restart, you would want to have the original executable act as a CLR host and load the target WPF application in a new AppDomain. When that AppDomain exits unexpectedly, you can repeat the process and restart the WPF app in another new AppDomain.

andrewbadera
  • 1,372
  • 9
  • 19