How to get timer running even when application is running in background or phone is locked in Windows Phone

StackOverflow https://stackoverflow.com/questions/21072975

Question

I have a timer in my application in Windows Phone 7.1 implemented using

DispatcherTimer _timer;

Initialized as

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

Here is my problem

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

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..

Was it helpful?

Solution

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.

OTHER TIPS

I searched for your question on google (cuz I'm not into Winphone) and found

http://stackoverflow.com/questions/8352515/how-can-i-run-my-windows-phone-application-in-background

apparently it simply is not possible. I hope this answers your question

Please write below line timer Initialization

ApplicationIdleModeHelper.Current.HasUserAgreedToRunUnderLock = true;

I resolved this issue by saving the starting timer of the value on isolated storage

 IsolatedStorageSettings.ApplicationSettings.Add("TimerStarted",DateTime.UtcNow);

And when the app is reactivated after going to background, i will look for this value in isolated storage and use that to show the timer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top