?
Windows 10 創意者更新之后,默認開啟了重啟后恢復應用程序狀態的功能。這是自 Vista 以來就提供的功能——Restart Manager。
應用程序實現這一功能只需要調用 RegisterApplicationRestart
即可。傳入兩個參數:
- 重啟后使用的命令行參數(例如當前正在打開的文件,以及正在閱讀或編輯的位置)
- 決定是否進行重啟的限制標記(任何時候都能重啟還是在某些條件下關掉重啟功能)
我封裝了以下這個函數的調用并將其放到 GitHub 上:sharing-demo/ApplicationRestartManager.cs at master · walterlv/sharing-demo。
調用代碼如下:
if (ApplicationRestartManager.IsRestartManagerSupported)
{ApplicationRestartManager.RegisterApplicationRestart(currentOpeningFile,ApplicationRestartFlags.None);
}
附:封裝的 ApplicationRestartManager
:
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Windows;
using Jetbrains.Annotations;namespace Walterlv.Win32
{/// <summary>/// 為應用程序提供重啟后恢復狀態的功能。/// </summary>public class ApplicationRestartManager{/// <summary>/// 獲取一個值,該值指示當前操作系統環境是否支持 Restart Manager 功能。/// Restart Manager 是 Windows Vista 新增的功能。在 Windows 10 秋季創意者更新之后,默認開啟了 EWX_RESTARTAPPS。/// </summary>public static bool IsSupported => IsSupportedLazy.Value;/// <summary>/// 向操作系統的 Restart Manager 注冊應用終止后的重啟方式。/// </summary>/// <param name="pwsCommandLine">/// 應用程序的重啟時應該使用的參數,允許為 null,表示不帶參數。/// 請注意:如果命令行參數中的某一個參數包含空格,請加上引號。/// </param>/// <param name="dwFlags">為應用程序的重啟行為添加限制,默認沒有限制。</param>/// <returns></returns>public static bool RegisterApplicationRestart([CanBeNull] string pwsCommandLine,ApplicationRestartFlags dwFlags = ApplicationRestartFlags.None){return 0 == RegisterApplicationRestart(pwsCommandLine, (int) dwFlags);}/// <summary>/// Windows Vista 及以上才開啟 Restart Manager。/// </summary>[ContractPublicPropertyName(nameof(IsSupported))]private static readonly Lazy<bool> IsSupportedLazy =new Lazy<bool>(() => Environment.OSVersion.Version >= new Version(6, 0));/// <summary>/// Registers the active instance of an application for restart./// See also: [RegisterApplicationRestart function (Windows)](https://msdn.microsoft.com/en-us/library/windows/desktop/aa373347)/// </summary>/// <param name="pwzCommandline">/// A pointer to a Unicode string that specifies the command-line arguments for the application when it is restarted. The maximum size of the command line that you can specify is RESTART_MAX_CMD_LINE characters. Do not include the name of the executable in the command line; this function adds it for you./// If this parameter is NULL or an empty string, the previously registered command line is removed. If the argument contains spaces, use quotes around the argument./// </param>/// <param name="dwFlags">/// This parameter can be 0 or one or more of the following values:/// - 1: Do not restart the process if it terminates due to an unhandled exception./// - 2: Do not restart the process if it terminates due to the application not responding./// - 4: Do not restart the process if it terminates due to the installation of an update./// - 8: Do not restart the process if the computer is restarted as the result of an update./// </param>/// <returns>/// This function returns S_OK on success or one of the following error codes./// - E_FAIL: Internal error./// - E_INVALIDARG: The specified command line is too long./// </returns>[DllImport("kernel32.dll", CharSet = CharSet.Auto)]private static extern uint RegisterApplicationRestart(string pwzCommandline, int dwFlags);}/// <summary>/// 表示重啟時的限制標記。/// </summary>[Flags]public enum ApplicationRestartFlags{/// <summary>/// 沒有重啟限制。如果僅指定 <see cref="None"/>,那么操作系統在可以重啟應用程序的時候都會重啟應用。/// </summary>None = 0,/// <summary>/// 指定此時不重啟:因未處理的異常而導致進程停止工作。/// </summary>RestartNoCrash = 1,/// <summary>/// 指定此時不重啟:因應用程序無響應而導致進程停止工作。/// </summary>RestartNoHang = 2,/// <summary>/// 指定此時不重啟:因應用安裝更新導致進程關閉。/// </summary>RestartNoPatch = 4,/// <summary>/// 指定此時不重啟:因操作系統安裝更新后重啟導致進程關閉。/// </summary>RestartNoReboot = 8}
}
參考資料
- 為何win10 1709(秋季創意更新) 重啟會自動恢復一些程序為重啟以前的工作狀態? - 蔣晟的回答 - 知乎
- RegisterApplicationRestart function (Windows)
- pinvoke.net: RegisterApplicationRestart (kernel32)
- Restart Manager Support For Windows Application - CodeProject
- c# - Restart a crashed program with RegisterApplicationRestart without user prompt - Stack Overflow
- Vista Application Crash Recovery in C# - CodeProject
- c# - Restart a crashed program with RegisterApplicationRestart without user prompt - Stack Overflow