开发者

Autorestart crashed WPF application

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 kn开发者_StackOverflow中文版ow 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


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.


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜