/// <summary>
        /// 向WER注册应用程序重启机制
        /// </summary>
        /// <param name="pwsCommandLine">重启命令行参数</param>
        /// <param name="restartFlag">重启标志,参考RestartFlags</param>
        /// <returns>0表示成功</returns>
        public static int RegisterApplicationRestart(string pwsCommandLine, CrashRecoveryApi.RestartFlags restartFlag)
        {
            if (!IsWindowsVistaOrLater)
            {
                return(-1);
            }
            var result = CrashRecoveryApi.RegisterApplicationRestart(pwsCommandLine, restartFlag);

            return(result);
        }
        /// <returns>0 if successfully unRegistered for restart notification</returns>
        public static uint UnRegisterForRestart()
        {
            if (!IsWindowsVistaOrLater)
            {
                return(1);
            }
            CrashRecoveryApi.UnregisterApplicationRecoveryCallback();
            var result = CrashRecoveryApi.UnregisterApplicationRestart();

            return(result);
        }
        /// <summary>
        /// This is the callback function that is executed in the event of the application crashing.
        /// </summary>
        /// <param name="pvParameter"></param>
        /// <returns></returns>
        private static int HandleApplicationCrash(IntPtr pvParameter)
        {
            //Allow the user to cancel the recovery.  The timer polls for that cancel.
            using (System.Threading.Timer t = new System.Threading.Timer(CheckForRecoveryCancel, null, 1000, 1000))
            {
                //Handle this event in your own code
                OnApplicationCrashed?.Invoke();

                CrashRecoveryApi.ApplicationRecoveryFinished(true);
            }

            return(0);
        }
        /// <summary>
        /// Registers the application for notification by windows of a failure.
        /// </summary>
        /// <returns>0 if successfully registered for restart notification</returns>
        public static uint RegisterApplicationRecoveryCallback()
        {
            if (!IsWindowsVistaOrLater)
            {
                return(1);
            }
            //  By default, the interval is 5 seconds (RECOVERY_DEFAULT_PING_INTERVAL). The maximum interval is 5 minutes.
            _recoverApplication = new ApplicationRecoveryCallback(HandleApplicationCrash);
            IntPtr ptrOnApplicationCrash = Marshal.GetFunctionPointerForDelegate(_recoverApplication);
            var    result = CrashRecoveryApi.RegisterApplicationRecoveryCallback(ptrOnApplicationCrash, IntPtr.Zero, (int)TimeSpan.FromMinutes(5).TotalMilliseconds,
                                                                                 0);

            return(result);
        }