日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

在操作系统重启后恢复应用程序的工作状态

發布時間:2023/12/18 windows 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在操作系统重启后恢复应用程序的工作状态 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

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

    轉載于:https://www.cnblogs.com/walterlv/p/10236497.html

    總結

    以上是生活随笔為你收集整理的在操作系统重启后恢复应用程序的工作状态的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。