【问题标题】:Windows phone 8.1 (WinRT) and Windows phone 10 (UWP) app suspending procedureWindows phone 8.1 (WinRT) 和 Windows phone 10 (UWP) 应用程序挂起程序
【发布时间】:2016-09-05 21:09:48
【问题描述】:

我在 windows 商店中发布了 windows 8.1/windows phone 8.1 (WinRT) 应用程序。该应用程序使用Windows.UI.Xaml.Application.Suspending 事件处理程序将重要的应用程序状态和统计信息保存到一组文件中(出于某种原因不使用内置设置 API)。为了成为“好孩子”,我尽量遵守Windows.ApplicationModel.SuspendingOperation.Deadline 并尽可能明确地调用Windows.ApplicationModel.SuspendingOperation.GetDeferral()。

以下是问题:

  1. 必须在截止日期之前调用Windows.ApplicationModel.SuspendingDeferral.Complete() 方法是否正确?

  2. 如果我未能在截止日期前致电,是否会有任何明显的处罚?从某些系统观察者的角度来看,它是否会使我的应用“无响应”?

  3. 在调用Windows.ApplicationModel.SuspendingDeferral.Complete() 时仍在运行的线程会发生什么情况?他们会被冻结还是什么?

  4. “异步”线程(即隐式 TPL 线程或ThreadPool.RunAsync)与 UI 线程在应用挂起过程中的行为是否存在差异?

  5. 在调用 Windows.ApplicationModel.SuspendingDeferral.Complete() 之前,我是否有义务明确取消所有异步任务,即我拥有的 System.Threading.Tasks.Task 实例?

【问题讨论】:

    标签: c# windows-runtime windows-phone-8.1 win-universal-app


    【解决方案1】:

    这是一篇关于 RT 应用生命周期的非常好的文章:What You Need To Know About Suspend and Resume in WinRT。

    如果您在 SuspendingOperation.Deadline 过期之前未调用 Complete 方法,则会发生超时,并且 Windows 会猜测您的应用发生了不良事件……并且,它不会被挂起,而是被终止。

    所以,你应该在 Dedline 之前完成你的工作并打电话给 Complete。

    【讨论】:

    • 感谢您的精彩文章。它说如果我得到 Deferral 并且在截止日期之前不调用 Complete 方法,我的应用程序将被终止,但它没有说明在截止日期之前完成我的任务。它说我现有的线程只会在挂起时被冻结,不会终止。您所说的“如果您没有在截止日期前完成工作 - 您的应用将被终止”是什么意思?
    • 如果您能在截止日期前完成,线程将在暂停时被冻结。
    • 再说一遍,“...在截止日期前完成工作 - 你的应用将被终止”是什么意思?
    • The SuspendingEventArgs SuspensionOperation object received by this handler provides the time in the future after which Windows will terminate your app, regardless of whether you have finished saving your state! This Deadline property is readonly: it is just a hint for you.
    • If you don’t call Complete method before the SuspendingOperation.Deadline expires, a timeout occurs and Windows guesses that something bad happened to your app… and, instead of being suspended, it will be terminated.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2014-09-07
    • 2016-04-15
    • 2015-07-01
    • 2015-02-25
    • 1970-01-01
    相关资源
    最近更新 更多