/// <summary>
 /// Called by an application's RecoveryCallback method to indicate that the recovery work is complete.
 /// </summary>
 /// <remarks>
 /// This should be the last call made by the RecoveryCallback method because
 /// Windows Error Reporting will terminate the application after this method is invoked.
 /// </remarks>
 /// <param name="success">true to indicate the the program was able to complete its recovery
 /// work before terminating; otherwise false</param>
 private static void ApplicationRecoveryFinished(bool success)
 {
     if (OSVersion.IsAboveOrEqual(WindowsVersion.Vista))
     {
         AppRestartRecoveryNativeMethods.ApplicationRecoveryFinished(success);
     }
 }
        /// <summary>
        /// Removes an application's restart registration.
        /// </summary>
        private static void UnregisterApplicationRestart()
        {
            if (OSVersion.IsAboveOrEqual(WindowsVersion.Vista))
            {
                HResult hr = AppRestartRecoveryNativeMethods.UnregisterApplicationRestart();

                if (hr == HResult.Fail)
                {
                    throw new ExternalException("Unregister for restart failed.");
                }
            }
        }
        /// <summary>
        /// Registers an application for automatic restart if the application is terminated by Windows Error Reporting.
        /// </summary>
        /// <param name="settings">An object that specifies the command line arguments used to restart the
        /// application, and  the conditions under which the application should not be  restarted.</param>
        /// <remarks>A registered application will not be restarted if it executed for less than 60 seconds before terminating.</remarks>
        private static void RegisterForApplicationRestart(RestartSettings settings)
        {
            if (OSVersion.IsAboveOrEqual(WindowsVersion.Vista))
            {
                HResult hr = AppRestartRecoveryNativeMethods.RegisterApplicationRestart(settings.Command, settings.Restrictions);

                switch (hr)
                {
                case HResult.Fail:
                    throw new InvalidOperationException("Application failed to registered for restart.");

                case HResult.InvalidArgument:
                    throw new ArgumentException("Failed to register application for restart due to bad parameters.");
                }
            }
        }
        /// <summary>
        /// Called by an application's RecoveryCallback method
        /// to indicate that it is still performing recovery work.
        /// </summary>
        /// <returns>A Boolean value indicating whether the user canceled the recovery.</returns>
        private static bool ApplicationRecoveryInProgress()
        {
            if (OSVersion.IsAboveOrEqual(WindowsVersion.Vista))
            {
                bool canceled;

                HResult hr = AppRestartRecoveryNativeMethods.ApplicationRecoveryInProgress(out canceled);

                if (hr == HResult.Fail)
                {
                    throw new InvalidOperationException("This method must be called from the registered callback method.");
                }

                return(canceled);
            }

            return(true);
        }
        /// <summary>
        /// Registers an application for recovery by Application Restart and Recovery.
        /// </summary>
        /// <param name="settings">An object that specifies the callback method, an optional parameter to pass to the callback
        /// method and a time interval.</param>
        /// <remarks>The time interval is the period of time within which the recovery callback method calls
        /// the ApplicationRecoveryInProgress method to indicate that it is still performing recovery work.</remarks>
        private static void RegisterForApplicationRecovery(RecoverySettings settings)
        {
            if (OSVersion.IsAboveOrEqual(WindowsVersion.Vista))
            {
                if (settings == null)
                {
                    throw new ArgumentNullException("settings");
                }

                GCHandle handle = GCHandle.Alloc(settings.RecoveryData);

                HResult hr = AppRestartRecoveryNativeMethods.RegisterApplicationRecoveryCallback(AppRestartRecoveryNativeMethods.internalCallback, (IntPtr)handle, settings.PingInterval, (uint)0);

                switch (hr)
                {
                case HResult.InvalidArgument:
                    throw new ArgumentException("Application was not registered for recovery due to bad parameters.");

                case HResult.Fail:
                    throw new ExternalException("Application failed to register for recovery.");
                }
            }
        }