即使应用程序在后台运行或手机在 Windows Phone 中被锁定,如何让计时器运行 [英] How to get timer running even when application is running in background or phone is locked in Windows Phone

查看:35
本文介绍了即使应用程序在后台运行或手机在 Windows Phone 中被锁定,如何让计时器运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用

DispatcherTimer _timer;

初始化为

Sample._timer = new DispatcherTimer();
Sample._timer.Interval = new TimeSpan(0, 0, 1);
Sample._timer.Tick += new EventHandler(Timer_Tick);
Sample._timer.Start();

    private void Timer_Tick(object sender, EventArgs e)
    {

        double newValue = Sample.Value + 1.686;
        if (newValue >= 100)
            newValue = 0;
        Sample.Value = newValue;
        txtDigitalClock.Text = GetTime();
    }
    public string GetTime()
    {
        time += TimeSpan.FromSeconds(1);
        return string.Format("{0:D2}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
    }

这在正常情况下工作正常

This is working fine in normal condition

这是我的问题

1) 当手机处于锁定状态(屏幕被锁定)时定时器没有运行

1) Timer is not running when phone is in locked state(screen is loced)

2) 当应用程序在后台运行时,计时器没有运行(当您在 Windows 手机中按开始按钮时,应用程序会进入后台).

2) Timer is not running when application is running in background (When you press start button in windows phone the app goes to background).

任何帮助将不胜感激..

any help would be greatly appreciated..

推荐答案

要在锁定屏幕下运行您的应用程序(和计时器),您必须禁用 ApplicationIdleDetectionMode.
如果您不禁用空闲,您的应用程序将停止,如 MSDN:

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

如果您想在后台运行计时器(例如,在按下开始"按钮后),您将无法像 MSDN 说:

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

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

您可以尝试使用 后台代理,但那是另外一回事了.

还要记住认证要求 当您的应用禁用空闲或使用后台代理时.

类似的问题是这里.

To run your App (and Timer) under lock screen, you have to disable ApplicationIdleDetectionMode.
If you don't disable idle your App 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 Timer in the background (eg. after pressing Start buton), you won't be able to do this 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 to do your job with Background Agents, but that is other story.

Also remember about Certification requirements when your App disables Idle or uses Background Agent.

Similar problem was here.

这篇关于即使应用程序在后台运行或手机在 Windows Phone 中被锁定,如何让计时器运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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