在作業系統重啟後恢復應用程式的工作狀態
阿新 • • 發佈:2019-02-10
Windows 10 創意者更新之後,預設開啟了重啟後恢復應用程式狀態的功能。這是自 Vista 以來就提供的功能——Restart Manager。
應用程式實現這一功能只需要呼叫 RegisterApplicationRestart
即可。傳入兩個引數:
- 重啟後使用的命令列引數(例如當前正在開啟的檔案,以及正在閱讀或編輯的位置)
- 決定是否進行重啟的限制標記(任何時候都能重啟還是在某些條件下關掉重啟功能)
呼叫程式碼如下:
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
}
}