0

I just got tripped up by this in a Windows Store app, here is a simplified example:

App.cs: Global timer

MainPage: Subscribes to Global Timer, button to create a local timer, button to goto page 2

Page2: Subscribes to Global Timer, button to create local timer, back button

On MainPage, it click the button to create a local timer. I see it and the global timer ticking.

Goto page 2. Still see ticks coming from subscribed global timer and local timer on Main Page. Start the local timer on page 2.

Go back, on MainPage. Now see 5 timers running:

The original MainPage subscribed to Global Timer
the original MainPage subscribed to local timer
A new subscription to Global Timer
The Page2 subscribed to Global Timer
The Page2 subscribed to it's local timer

I realize I can use the navigatedto/from events to subscribe/unsubscribe from the global timer. A bit of a pain because in my real example I can subscribe to dozens of different events in different pages from the global class. A little harder to find the timers created in the local timer button since there is no reference to it outside the button.

Are the subscribers keeping the previous pages alive or is this normal? I looked at NavigationCacheMode mode, by default creates a new instance each time. I assumed everything was basically cleared, which is why to have to save the state of textboxs, etc, when going between pages.

Is the only way around this to unsubscribe to all global events when navigating away, and to keep a class level variable so I have a reference to one off's like my "local timer"?

Jason
  • 245
  • 3
  • 12
  • Related: [Weak events in .NET?](http://stackoverflow.com/questions/1089309/weak-events-in-net) – chue x Sep 21 '15 at 23:20
  • 1
    The timer has its own thread (well, that's technically incorrect but close enough). That thread has a reference to your page (since you subscribed to one of its events) and is therefore keeping your page alive. That's a common source of leaks. Make sure to unsubscribe from all timers when leaving the page. – Kevin Gosse Sep 22 '15 at 06:17

1 Answers1

0

Thanks to KooKiz for the answer, using timers as a quick test seems to have been a red herring. I'll just unsubscribe all events from global class when navigating way, as well as any local timers, and not worry about anything else.

Jason
  • 245
  • 3
  • 12