如何禁用 UWP 应用程序暂停? [英] How to Disable UWP App Suspension?

查看:60
本文介绍了如何禁用 UWP 应用程序暂停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 为 Windows 10 开发仅在台式计算机上运行的 UWP 应用程序,目标平台版本为 10.0.14393.0.由于业务需求,应用生命周期必须像传统的 Win32 应用一样运行.

I am using C# to develop a UWP app for Windows 10 running only on desktop computers, targeting platform version 10.0.14393.0. Due to business requirements, the app lifecycle must behave like a traditional Win32 application.

因此,我遵循了 这篇文章 请求带有 ExtendedExecutionReason.UnspecifiedExtendedExecutionSession.我还将 Windows 配置为从不休眠和从不休眠.

Therefore, I followed the recommendation from this article to request an ExtendedExecutionSession with ExtendedExecutionReason.Unspecified. I also configured Windows to never sleep and never hibernate.

不过,在极少数情况下,Windows 会以 SystemPolicy 为由撤销扩展执行会话,然后继续暂停 UWP 应用.

Still, on rare occasions, Windows will revoke the extended execution session with reason SystemPolicy and then proceed to suspend the UWP app.

两个问题:

  1. 如何获取有关导致 Windows 撤销扩展执行会话的更多信息(系统日志?事件日志?)?
  2. 我如何才能摆脱这些罕见的暂停情况,以便 UWP 应用生命周期的行为与 Win32 应用完全一样(即保持运行直到用户明确停止运行)?

谢谢!

推荐答案

ExtendedExecutionSessionExtendedExecutionReason.Unspecified 是受多个资源消耗限制的约束.由于您的测试设备没有电池,因此您的应用程序挂起的最可能原因是其高内存使用.您可以尝试在内存消耗方面优化应用程序并使用 内存管理 API 如文档所示,但这仍然不能保证您的应用永远不会被暂停.

ExtendedExecutionSession with ExtendedExecutionReason.Unspecified is a subject to multiple restrictions regarding resource consumption. Since your test device doesn't have a battery, then the most likely reason for your app getting suspended is its high memory use. You can try to optimize the app in terms of memory consumption and make use of Memory Management APIs as documentation suggests, but still this doesn't guarantee that your app will never get suspended.

如果您的应用面向商业领域,那么您可以考虑使用更强大的ExtendedExecutionForegroundSession 而不是 ExtendedExecutionSession.这可能是您问题的完美解决方案.但这是一项受限功能,这意味着 Windows 应用商店不允许使用它的应用程序 - 仅适用于 Windows 应用商店.您还需要在清单中手动声明 extendedExecutionUnconstrained 功能(请参阅 文档的特殊和受限功能部分) 以利用 API.

If your app is aimed at business sector then you might consider using more powerful ExtendedExecutionForegroundSession instead of ExtendedExecutionSession. This would probably be the perfect solution for your problem. But it's a restricted capability, which means an app that utilizes it is not allowed to Windows Store - only to Windows Store for Business. You'd also need to manually declare the extendedExecutionUnconstrained capability in the manifest (see the Special and restricted capabilities section of documentation) to take advantage of the API.

或者,您可以使用黑客来防止应用长时间暂停:

Alternatively you can use hacks that prevent app from getting suspended for long periods of time:

  1. 使用应用服务 用于按照 pnp0a03 的建议与 Win32 应用程序通信.

  1. Use of App services for communicating with Win32 apps as pnp0a03 suggested.

使用背景媒体播放用于在后台无限循环播放静音音频样本(即使用户手动停止或其他应用暂停播放自己的背景音频,您的应用也可以跟踪并自动重启播放).

Use Background Media Playback for playing silent audio sample in the background infinitely in a loop (even if the user stops it manually or another app pauses it to play its own background audio, your app can trace it and automatically restart the playback).

这篇关于如何禁用 UWP 应用程序暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆