2

我有一个使用 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;
    }
}

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

4

1 回答 1

5

这是如何禁用程序崩溃时出现的窗口的解决方案。不仅是 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 );
    }
于 2012-05-19T23:08:30.200 回答