我有一个使用 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;
}
}
程序在未处理的异常上重新启动(肯定是模拟的)。但旧副本未关闭,系统消息“程序意外终止”。程序大部分时间不与用户交互,因此没有人可以关闭该消息以关闭程序。问题是如何默默地关闭那个旧窗户?