当Windows Phone 8应用程序处于后台模式时,DispatcherTimer将停止

时间:2014-01-03 14:41:59

标签: c# windows-phone-8

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

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

我怎么能做到这一点?顺便说一句我已经在使用:

PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;

由于

2 个答案:

答案 0 :(得分:2)

DispatcherTimer在UI线程上运行,因此当应用程序在后台运行时它会被停止是有意义的,您可以尝试使用其他计时器,例如Threading.Timer

答案 1 :(得分:1)

如果您的意思是在锁定屏幕下运行,如果您禁用ApplicationIdleDetectionMode,您的System.Windows.Threading.DispatcherTimer将与任何其他计时器一样工作。
如果您没有禁用空闲,它将在MSDN处停止时停止:

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

如果你想在后台运行(例如在按下Start buton之后),你将无法使用任何计时器执行此操作,因为MSDN说:

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.

更大的问题是当你的应用程序被逻辑删除时 - 应用程序不会在内存中占用(全部)。

您可以尝试Background Agents,但这是其他故事,它们可能不适合更新TextBlock。

修改

为什么不记住用户在PhoneApplicationService.Current.StateIsolatedStorageSettings中停用应用的时间,当用户再次激活时,根据该值计算差异并执行您想要的操作?

相关问题