我需要在 WPF 崩溃时自动重新启动它。我现在尝试了一些方法,但我的应用程序仍然崩溃并且没有捕获到错误。 第二个问题是我不知道它为什么会崩溃。好吧,我不知道异常,VS 没有告诉我。只是 vshost32.exe 已经停止工作。我所知道的是,如果我在短时间内在 MediaElement 中加载大量不同的文件,就会发生这种情况。 (可能是溢出异常之类的)
这是我的代码。它位于 App.xaml 中,并且删除了 XAML 中的“StartupUri”。
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");
}
编辑// 在windows的errorlog中发现异常: 大致翻译是这样的:
答案:newKnoblauch.exe Frameworkversion:v4.0.30319 描述:由于未处理的异常,进程已停止。异常信息:异常代码 c0000005,异常地址 55542ECC
Fehlerhaften Anwendung 的名称:newKnoblauch.exe,版本:1.0.0.0,时间:0x4d62e0a6 名称 des fehlerhaften 模块:wmp.dll,版本:12.0.7600.16667,时间:0x4c7dd593 异常代码:0xc0000005 错误偏移:0x00182ecc ID des fehlerhaften Prozesses: 0xa98
最佳答案
为了实现自动重启,您可能希望将原始可执行文件作为 CLR 宿主并在新的 AppDomain 中加载目标 WPF 应用程序。当该 AppDomain 意外退出时,您可以重复该过程并在另一个新的 AppDomain 中重新启动 WPF 应用程序。
关于c# - 自动重启崩溃的 WPF 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5071746/