0

This is a very strange problem, and all the people I had asked to confirm it said that it takes place.

I have a Threading.Timer instance which fires every 15 minutes. And if I call the PhotoChooser view and then select a photo from it, when going back to the calling page my application calls that timer's callback! I tried different timers either it be Timer from Threading namespace or Dispatcher timer.

The same happens when being in my app I hold the back button of my device and then choose the app from the list.

My application is as plain as it can be - the timer with a callback and method calling PhotoChooser. Could anyone help with solution or workaround please?

Update:

My code construction is as follows:

private Timer _timer;

public void CallTimer()
{
    var period = 15 * 1000 * 60;
   _timer = new Timer(repeatTimer_Tick, null, 0, period);
}

private void repeatTimer_Tick(object state)
{
   // Some action here
}

private void Stop()
{
   if (_timer != null)
     _timer.Dispose();
}

private void CallPhotoChooser()
{
   // Some basic actions calling photochooser task
}
1
  • 1
    The timer is most likely recreated when the page is navigated back. Are you creating the timer in the constructor of the page, or on the Loaded event?
    – Patrick
    Sep 9, 2012 at 11:29

2 Answers 2

2

As explained in Windows Phone 7 Tombstoning the application is most likely tombstoned when the user presses and holds the back button or a launcher, like the PhotoChooserTask, is called. This happens unless the page is returned to in a matter of seconds.

You need to store the timer timeout in your application state somehow, or set the initial timeout to 15 minutes so it doesn't fire immediately. To store the application state, take a look at the article linked, it recommends doing this in the NavigatedFrom event which you can overload in the page code behind.

The time left before the timer fires is a bit more difficult. I guess to know how long it's left of a timeout you need to get the time with DateTime.UtcNow (which you can store in the application state) when creating the timer and calculate the next time it will fire upon resuming the application.

1

You should not be creating those long running timers :) Just handle the activation/deactivation and reset your timers, then reinstate them when photo chooser returns you back to your app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.