locked
What happens to a page when it is navigated from RRS feed

  • Question

  • I am using the "LayoutAwarePage" as the base for my pages. In one of my pages I have a Timer as below.

    public sealed partial class ExamplePage : LayoutAwarePage
        {
            private DispatcherTimer _dispatcherTimer;
            public ExamplePage ()
            {
                this.InitializeComponent();
                _dispatcherTimer = new DispatcherTimer();
                _dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
                _dispatcherTimer.Tick += _guiUpdateTimer_Tick;
                _dispatcherTimer.Start();
            }
    }

    When I navigate to "ExamplePage" a Dispatcher Timer is created. If I leave the page and then navigate back to it a new instance of "ExamplePage" appears to be created along with a new DispatcherTimer.  The problem is the page that was created when I navigated to "ExamplePage" for the first time still seems to be loaded in memory somewhere.  This means I have now have a timer for every time I've visited the page.

    My question is, how should I be disposing of a page when I navigate away from it?  The multiple timer issue is causing some very undesirable effects on my UI but I'm also worried about the general memory impact of more and more pages being created as I navigate around my application.

     I notice in lots of the Examples from Microsoft they are overriding "OnNavigatedTo" And "OnNavigatedFrom" whereas when I create a "BasicPage" template, these seemed to have been dropped in favour of "LoadState" and "SaveState". Is there a difference and what should I be doing to dispose of my page once left (Or make sure the original instance is reloaded if still available)


    • Edited by StormENT Monday, September 3, 2012 9:00 PM
    Monday, September 3, 2012 8:54 PM

Answers

  • in loadstate your create the timer. in save state you stop and dispose the timer
    • Marked as answer by StormENT Monday, September 3, 2012 11:17 PM
    Monday, September 3, 2012 9:05 PM

All replies

  • in loadstate your create the timer. in save state you stop and dispose the timer
    • Marked as answer by StormENT Monday, September 3, 2012 11:17 PM
    Monday, September 3, 2012 9:05 PM
  • Thanks Dave,
    Just to check..... If I simply Stop the timer then is should be disposed of and I don't have to do anything else? Also is it only the timer that is hanging around because I hadn't stopped it. After stopping it in savestate is it a safe assumption that the rest of the Page will also be disposed of?

    Monday, September 3, 2012 9:14 PM
  • well yuo have to check if it implements IDisposable.. if so call Dispose, if not stop is enough.

    and yes page get cleaned up without you have to do something


    Monday, September 3, 2012 9:19 PM