使用RegisterApplicationRestart重新启动崩溃的程序,无需用户提示

Hug*_*une 13 c# windows crash restart windows-error-reporting

我正在使用Windows错误报告API调用RegisterApplicationRestart来注册应用程序,当应用程序崩溃或重新启动PC时,WER会自动重新启动该应用程序.

但是,当应用程序崩溃时,会弹出默认的WER对话框("xyz已停止响应"/"您是否要发送有关该问题的更多信息"),并且只有在关闭此对话框后,程序才会重新启动.

有没有办法压制这个对话框?

如果我调用SetErrorMode(SEM_NOGPFAULTERRORBOX),则会按预期禁止该对话框,但重启本身也会停止工作.

如果我通过更改注册表项全局禁用该对话框HKEY_CURRENT_USER\Software\ Microsoft\Windows\Windows Error Reporting\DontShowUI,我得到相同的结果:对话框被禁止,但应用程序也不会重新启动.

我知道像第二个看门狗程序那样的解决方法,但我真的希望使用Windows错误报告API的工具尽可能简单地解决这个问题.

use*_*830 14

您可以RegisterApplicationRecoveryCallback改为使用并重新启动流程.它不会禁止错误报告对话框,但可以在没有用户交互的情况下重新启动应用程序.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;

namespace Test
{
    class Program
    {
        public delegate int RecoveryDelegate(IntPtr parameter);

        [DllImport("kernel32.dll")]
        private static extern int RegisterApplicationRecoveryCallback(
                RecoveryDelegate recoveryCallback,
                IntPtr parameter,
                uint pingInterval,
                uint flags);

        [DllImport("kernel32.dll")]
        private static extern void ApplicationRecoveryFinished(bool success);

        private static void RegisterForRecovery()
        {
            var callback = new RecoveryDelegate(p=>
            {
                Process.Start(Assembly.GetEntryAssembly().Location);
                ApplicationRecoveryFinished(true);
                return 0;
            });

            var interval = 100U;
            var flags = 0U;

            RegisterApplicationRecoveryCallback(callback,IntPtr.Zero,interval,flags);
        }

        static void Main(string[] args)
        {
            RegisterForRecovery();

            for (var i = 3; i > 0; i--)
            {
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write("Crash in {0}", i);
                Thread.Sleep(1000);
            }
            Environment.FailFast("Crash.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过设置ErrorCode,SEM_NOGPFAULTERRORBOX我们正在更改异常过滤行为并强制它传递异常(EXCEPTION_CONTINUE_SEARCH)而不是弹出错误报告对话框(EXCEPTION_EXECUTE_HANDLER).

也许正确的方法(实际上防止错误报告对话框在大多数情况下弹出)将是使用SetUnhandledExceptionFilter并在那里进行恢复,在.Net中大致相当于使用AppDomain.CurrentDomain.UnhandledException.如果需要捕获Win32异常,我们应该通过向App config添加以下行来启用LegacyCorruptedStatePolicy.

<configuration>
   <runtime>
      <legacyCorruptedStateExceptionsPolicy enabled="true" />
   </runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但它不会捕获所有(例如Environment.FastFail或一些访问冲突),因此我建议使用两者.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;

namespace Test
{
    class Program
    {
        public delegate int RecoveryDelegate(IntPtr parameter);

        [DllImport("kernel32.dll")]
        private static extern int RegisterApplicationRecoveryCallback(
                RecoveryDelegate recoveryCallback,
                IntPtr parameter,
                uint pingInterval,
                uint flags);

        [DllImport("kernel32.dll")]
        private static extern void ApplicationRecoveryFinished(bool success);

        private static void RegisterForRecovery()
        {
            var callback = new RecoveryDelegate(p=>
            {
                Recover();
                ApplicationRecoveryFinished(true);
                return 0;
            });

            var interval = 100U;
            var flags = 0U;

            RegisterApplicationRecoveryCallback(callback,IntPtr.Zero,interval,flags);
        }

        private static void Recover()
        {
            //do the recovery and cleanup
            Process.Start(Assembly.GetEntryAssembly().Location);
        }

        private static unsafe void Crash1()
        {
            var p = (int*)0;
            p[0] = 0;
        }

        private static unsafe void Crash2()
        {
            var v = 1;
            var p =&v;
            p -= ulong.MaxValue;
            p[0] = 0;
        }

        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler((s, e) =>
                {
                    Recover();
                    Environment.Exit(1);
                });

            RegisterForRecovery();

            for (var i = 3; i > 0; i--)
            {
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write("Crash in {0}", i);
                Thread.Sleep(1000);
            }

            //different type of crash
            throw new Exception("Crash.");
            //Environment.FailFast("Crash.");
            //Crash1();
            //Crash2();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)