当 Windows Phone 8 应用程序处于后台模式时 DispatcherTimer 停止 [英] DispatcherTimer stops when Windows Phone 8 app is in background mode

查看:18
本文介绍了当 Windows Phone 8 应用程序处于后台模式时 DispatcherTimer 停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调度程序计时器正在运行,当我将应用程序发送到后台时,计时器会停止滴答作响"(并在应用程序重新激活时继续运行).

I have a dispatcher timer running, and when I send the app to the background the timer stops "ticking" (and continues to run when the app is reactivated).

当应用程序处于后台模式时,我需要计时器继续滴答作响.计时器正在更新 TextBlock.

I need the timer to continue ticking when the app is in background mode. The timer is updating a TextBlock.

我如何才能做到这一点?顺便说一下,我已经在使用了:

How I can achieve this? By the way I'm already using:

PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;

谢谢

推荐答案

如果你的意思是在锁屏下运行,如果你禁用 ApplicationIdleDetectionMode,你的 System.Windows.Threading.DispatcherTimer 将工作得和任何其他定时器.
如果您不禁用空闲,它将停止,如 MSDN:

如果设备的锁定屏幕处于启用状态,也会引发此事件(停用),除非禁用了应用程序空闲检测.

如果您想在后台运行(例如,按下开始"按钮后),您将无法使用任何计时器执行此操作,如 MSDN 说:

当用户向前导航离开应用程序时,在引发 Deactivated 事件后,操作系统将尝试将应用程序置于休眠状态.在这种状态下,应用程序的所有线程都停止并且不进行任何处理,但应用程序在内存中保持完整.

更大的问题是当您的应用程序被墓碑化时 - 该应用程序不会(全部)在内存中.

您可以尝试使用 后台代理,但那是另外一回事了,它们可能不适合更新 TextBlock.

编辑

以及为什么不记住用户在 PhoneApplicationService.Current.StateIsolatedStorageSettings 中停用应用程序的时间,当用户再次激活时,计算差异并执行您的操作想要,取决于那个值?

If you mean running under lock screen, if you disable ApplicationIdleDetectionMode, your System.Windows.Threading.DispatcherTimer will work as well as any other Timer.
If you don't disable idle it will stop as it is said at MSDN:

This event (Deactivation) is also raised if the device’s lock screen is engaged, unless application idle detection is disabled.

If you want to run in the background (eg. after pressing Start buton), you won't be able to do this with any timer as MSDN says:

When the user navigates forward, away from an app, after the Deactivated event is raised, the operating system will attempt to put the app into a dormant state. In this state, all of the application’s threads are stopped and no processing takes place, but the application remains intact in memory.

The bigger problem is when your App is tombstoned - the app doesn't ramain (all) in memory.

You can try with Background Agents, but that is other story and they probably won't be suitable to update TextBlock.

EDIT

And why not to remember the time when the user deactivates the App in PhoneApplicationService.Current.State or IsolatedStorageSettings, and when the user activates again, calculate the difference and do what you want, depending on that value?

这篇关于当 Windows Phone 8 应用程序处于后台模式时 DispatcherTimer 停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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