0

I'm working in a Xamarin Forms project, using Prism DryIoc. For example, i have a MainPage, and a APage, with ViewModels MainPageViewModel and APageViewModel. In APageViewModel i have a timer as simple as the code below:

private Timer _timer;
public override void Initialize(INavigationParameters parameters)
    {
        base.Initialize(parameters);

        _timer = new Timer(1000);

        _timer.Elapsed += (sender, args) =>
        {
            Debug.WriteLine($"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Timer run");
        };

        _timer.Start();
    }

When i navigate from MainPage to APage using NavigationService.PushAsync, the timer start. But when excute NavigationService.GoBackAsync from APageViewModel, the timer not stop if i dont manual stop the timer with _timer.Stop(). So i wonder if the APageViewModel being properly dispose to clear resources that being use in the view model?

  • What code do you expect to make the timer stop? None that you posted, and the framework cannot magically know that you started a timer, after all. If your view model implements `IDisposable` you should show that code, too. – Haukinger Aug 20 '19 at 10:43
  • I think you have to stop the timer manually as View-ViewModel won't be dispose when navigate back. – nevermore Aug 21 '19 at 03:26
  • Stop timer is simple. But my question is, do object ViewModel being dispose to clear resources? – dienbien001 Aug 21 '19 at 07:43
  • Have a look at these threads may help: [object-disposing-in-xamarin-forms](https://stackoverflow.com/questions/43494527/object-disposing-in-xamarin-forms/43551679) and [viewmodel-lifecycle-when-does-it-get-disposed](https://stackoverflow.com/questions/15961664/viewmodel-lifecycle-when-does-it-get-disposed). – nevermore Aug 21 '19 at 09:46
  • @JackHua-MSFT as far as I understand, if the view model implements `IDisposable`, Prism will call `Dispose` – Haukinger Aug 21 '19 at 10:48
  • @dienbien001 do you implement `IDisposable` or not? – Haukinger Aug 21 '19 at 10:49
  • @Haukinger yes, i do implemente `IDisposable`. – dienbien001 Aug 22 '19 at 02:08
  • @dienbien001 and where's the code? – Haukinger Aug 22 '19 at 06:12
  • Sorry for the late reply. Here is my code: https://github.com/dienbien001/AppPrismSample1/blob/master/AppPrismSample/AppPrismSample/ViewModels/APageViewModel.cs. Just call _timer.Stop() from Destroy method can stop the timer. But i wonder will the timer be clear from resource, or i have to call call `_timer = null` in Destroy method. – dienbien001 Aug 23 '19 at 08:28
  • @Haukinger: I've just add IDisposable to ViewModel, but the Dispose not being called. – dienbien001 Aug 23 '19 at 10:19
  • @dienbien001you can try to Implement IDestructible interface in APageViewModel and implement method Destroy...Later you can add debugger pointer to verify if Dispose was called or not – Hamid Shaikh Sep 10 '19 at 08:01
  • @HamidShaikh thanks you. That's the solution we're doing right now. – dienbien001 Sep 12 '19 at 02:46
  • @dienbien001 Great, Please share result once you are done with your analysis – Hamid Shaikh Sep 12 '19 at 07:01

1 Answers1

0

Have you tried to stop the timer on the OnDisappearing method of the page?

lavilaso
  • 27
  • 9
  • My question is not "how to stop timer", but do ViewModel object being disposed when navigate back to clear resources? – dienbien001 Aug 21 '19 at 07:44