我有一个使用 AciveX 对象并提供 WCF 服务的 C# WPF 程序。所以有很多线程,很多对非托管代码的调用。有时在满载情况下应用程序会崩溃。我正在调查这个问题 - 看起来问题出在 ActiveX 中的某个地方,我无法更改。无论如何,我需要一种在崩溃后恢复程序的方法。目前我已经找到解决方案:
public partial class App : Application
{
private static readonly Logger log = LogManager.GetCurrentClassLogger();
bool isClosing = false;
[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute]
void D_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
log.FatalException("Error UnhandledException ", (Exception)e.ExceptionObject);
MonitoringSvc.host.Close();
log.Info("Restarting");
if (!isClosing)
{
System.Windows.Forms.Application.Restart();
System.Windows.Application.Current.Shutdown();
}
}
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain D = AppDomain.CurrentDomain;
D.UnhandledException += new UnhandledExceptionEventHandler(D_UnhandledException);
}
private void Application_Exit(object sender, ExitEventArgs e)
{
isClosing = true;
}
}
程序在未处理的异常上重新启动(当然是在模拟时)。但旧副本仍未关闭,并显示系统消息“程序意外终止”。程序大多数时候不与用户交互,因此没有人可以关闭该消息来关闭程序。问题是如何默默地关闭旧窗口?
最佳答案
这是如何禁用程序崩溃时出现的窗口的解决方案。不仅是 WER,因为即使禁用了 WER,也会显示其他窗口(要求关闭或调试程序)。
[DllImport("kernel32.dll", SetLastError = true)]
static extern int SetErrorMode(int wMode);
[DllImport("kernel32.dll")]
static extern FilterDelegate SetUnhandledExceptionFilter(FilterDelegate lpTopLevelExceptionFilter);
public delegate bool FilterDelegate(Exception ex);
App()
{
FilterDelegate fd = delegate(Exception ex)
{
return true;
};
SetUnhandledExceptionFilter(fd);
SetErrorMode(SetErrorMode(0) | 0x0002 );
}
关于c# - 如何防止显示 "program terminated Unexpectedly"窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10665288/