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

 

Windows 10 创意者更新之后,默认开启了重启后恢复应用程序状态的功能。这是自 Vista 以来就提供的功能——Restart Manager。


应用程序实现这一功能只需要调用 RegisterApplicationRestart 即可。传入两个参数:

  1. 重启后使用的命令行参数(例如当前正在打开的文件,以及正在阅读或编辑的位置)
  2. 决定是否进行重启的限制标记(任何时候都能重启还是在某些条件下关掉重启功能)

我封装了以下这个函数的调用并将其放到 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/453564.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

裁员感悟

好员工&#xff0c;别以为裁员与你无关(上) 版权声明&#xff1a;原创作品&#xff0c;允许转载&#xff0c;转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。本文地址&#xff1a;http://blog.csdn.net/jobchanceleo/archive/2007/05/26/…

php传中文给Java_完美解决PHP中文乱码(转) - - JavaEye技术网站

PHP中文乱码一般是字符集问题&#xff0c;编码主要有下面几个问题。一&#xff0e;首先是PHP网页的编码1.文件本身的编码与网页的编码应匹配a.如果欲使用gb2312编码&#xff0c;那么php要输出头&#xff1a;header(“Content-Type: text/html; charsetgb2312")&#xff0c…

CharSequence类

CharSequence是char类型的一个可读序列&#xff0c;它本身是一个接口&#xff0c;CharBuffer、String、StringBuffer、StringBuilder这个四个类实现了这个接口。此接口对于不同种类的char序列提供统一的只读访问以下是这个函数的API 它只定义了四个方法 /*** This interface re…

程序员考核的五大死因

程序员考核的五大死因&#xff08;上&#xff09; 程序员作为企业开发力量的最核心资产&#xff0c;无疑得到公司从上至下的一致关注。开发是个智力密集型产业&#xff0c;程序开发的特点是&#xff0c;付出相同时间的情况下&#xff0c;两个开发者之间的产能会相差十几甚至几…

java编写螺旋矩阵讲解_Java如何实现螺旋矩阵 Java实现螺旋矩阵代码实例

本篇文章小编给大家分享一下Java实现螺旋矩阵代码实例&#xff0c;小编觉得挺不错的&#xff0c;现在分享给大家供大家参考&#xff0c;有需要的小伙伴们可以来看看。给定一个包含 m x n 个元素的矩阵(m 行, n 列)&#xff0c;请按照顺时针螺旋顺序&#xff0c;返回矩阵中的所有…

Vue Axios的配置 (高仿饿了么)

export default {name: "app",components: {"v-header": header},data() {return {seller: {}};},created() {let _this this; // 让this始终代表最初this指向的对象this.axios.get(../data.json).then(function(res) {_this.seller res.data.sellercons…

PagerAdapter学习

前言: ViewGroup可以实现很多功能&#xff0c;如简单的页面导航和页面滑动等等。谷歌公司为我们提供ViewGroup的API。谷歌公司推荐我们把ViewGroup和Fragment一起使,如果一起使用的话&#xff0c;应该使用FragmentPagerAdapter和FragmentStatePagerAdapter来进行适配处理&#…

arXiv网站

arXiv 原先是由物理学家保罗金斯巴格在1991年建立的网站&#xff0c; 我们会将预稿上传到arvix作为预收录&#xff0c;因此这就是个可以证明论文原创性&#xff08;上传时间戳&#xff09;的文档收录网站。转载于:https://www.cnblogs.com/AntonioSu/p/8387324.html

加薪——爱你在心口难开

加薪——爱你在心口难开(1) &#xff08;原文刊登于《程序员》杂志07年第4期&#xff09; 刚过了春节的4月份&#xff0c;空气中到处透着躁动的味道&#xff0c;“求职”、“招聘”不断刺激着程序员们的耳鼓&#xff0c;其实大多数跳槽者如果能静下心来审视自己一下&#xf…

java线程interrupt用法_Java线程中interrupt那点事 | 学步园

1.先看一下例子程序&#xff1a;import java.io.IOException;import java.net.ServerSocket;import javax.rmi.CORBA.Tie;/**author: ZhengHaibo*web: http://blog.csdn.net/nuptboyzhb*mail: zhb931706659126.com*2014-3-16 Nanjing,njupt,China*/public class TestThread {/*…

Kotlin Native新增Objective-C互操作能力以及对WebAssembly的支持

根据JetBrains技术主管Nikolay Igotti的介绍&#xff0c;Kotlin/Native 0.4已经可用于为iOS和macOS开发原生应用。此外该版本还为WebAssembly平台提供了实验性支持。 \\Kotlin/Native对iOS/macOS开发的支持&#xff0c;关键在于实现了与Objective-C的互操作性。JetBrains目前已…

TranslateAnimation动画

众所周知&#xff0c;TranslateAnimation是android中重要的一个动画函数&#xff0c;很多时候我们都需要使用它来实现更好的UI效果&#xff0c;今天就简单研究下这个TranslateAnimation。TranslateAnimation这个位移动画主要有三个构造函数&#xff0c;对应着三种不同的参数形式…

maven项目使用jacoco插件检测代码覆盖率详细配置

使用maven构建项目&#xff08;java项目或者web项目都可以&#xff09; jacoco插件的配置参考官方网址&#xff1a;http://www.eclemma.org/jacoco/trunk/doc/maven.html &#xff08;1&#xff09;配置jacoco的依赖jar包 <dependency><groupId>org.jacoco</gro…

JAVA如何取得空list成员类型_String 类型的List作为一个成员变量保存,保存成功后取对象时报空指针...

异常&#xff1a;Caused by: java.lang.NullPointerException at org.litepal.crud.DataHandler.setToModelByReflection(DataHandler.java:1341) at org.litepal.crud.DataHandler.setGenericValueToModel(DataHandler.java:787) at org.litepal.crud.DataHandler.query(DataH…

C语言:几种字符输入函数的区别

几种字符输入函数的区别&#xff1a; 1、getche()函数:用于从键盘读入一个字符并显示&#xff0c;然后直接执行下一条语 句。2、getch()函数:用于从键盘中读入一个字符&#xff0c;但不显示在屏幕上&#xff0c;然后执行下一条语句。3、getchar()函数&#xff1a;用于从键盘读…

VCG Mesh刚性旋转(变换矩阵)

文章目录 一、简介二、实现代码三、实现效果参考资料一、简介 旋转矩阵如果从线性空间的角度来看,它类似于一个投影过程。假设坐标 P ( x 1 , y 1 , z 1 ) P(x_1,y_1,z_1)

薪水增长多少,新机会才值得考虑?

薪水增长多少,新机会才值得考虑? 阴历年马上就要来到&#xff0c;猪年正在向我们招手。相信有些朋友年后考虑新的要作机会&#xff0c;年终奖和第13个薪水已到手&#xff0c;是考虑一下离开这个让自己不“爽”公司的时候了&#xff0c;哈哈&#xff01; 但是&#xff0c;薪水…

ScaleAnimation动画

ScaleAnimation动画是用来进行缩放的动画&#xff0c;我在使用时刚开始有些不解的问题后来经过学习&#xff0c;有了一个更深的了解。先来看看源码&#xff0c;其实ScaleAnimation有四个构造函数&#xff0c;这里我只列出了其中的一个&#xff0c;因为另外的三个其实都只是这个…

Swift快速入门(一)第一个Swift程序

1. 本系列说明 本系列只是一个Swift快速入门的教程&#xff0c;并没有详尽的介绍Swift&#xff0c;Swift也并不是一个简单的编程语言&#xff0c;所以要想详尽的系统的学习Swift&#xff0c;本系列并不适合你&#xff0c;此系列只是让开发者可以快速的用Swift来进行开发。另外学…

java 判断数字变化增减_java String 强化操作 判断数字 字符串转阿拉伯数字,相似度等等...

importjava.io.BufferedReader;importjava.io.StringReader;importjava.util.ArrayList;importjava.util.List;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/***author*/public classStrings {/*** 全角转半角**paramsbc 全角字符*returnString*/public stat…