Reputation: 77
I previously created a timer and stopwatch app for Windows Phone 8.1, but it does not run in the background or behind lockscreens. I am currently updating this for windows 10 universal app and could do with some help to implement these features.
Upvotes: 3
Views: 1103
Reputation: 1582
It might be tempting to try do this in a background task, but that is very heavy-handed, for a simple problem.
What I would suggest is to record a DateTime of when they started recording:
DateTime _startDateTime;
void Start()
{
_startDateTime = DateTime.Now;
}
Then do your regular timing code (presumably with a DispatcherTimer
) which will run a method every x milliseconds:
void UpdateTime()
{
TimeSpan totalTime = DateTime.Now.Subtract(_startDateTime);
// Update UI
}
Now, if the user minimizes the app, code will stop running, however when you return, the math in UpdateTime()
is using an absolute value of _startDateTime
so it doesn't matter if you were minimized for an hour or a week - as soon as UpdateTime()
runs it will correctly work out the elapsed time.
Unless they minimize and change the time on the phone, but there really isn't much you can do about that ;-).
Upvotes: 3