Bright Lee
Bright Lee

Reputation: 2326

Xamarin.Forms Timer doesn't skip UI logic even when it's not showing?

I'm making a app with using XF PCL.

I started to doubt that XF's timer is not good enough for UI routine. for example, in iOS native app, If View is not showing some reason (new page's pushed or something), scheduler is stopped automatically. Because it's for UI.

But Xamarin's doesn't seem like that. It's still doing his job.

I hope at least within "Device.BeginInvokeOnMainThread(() =>" will be skipped when it's disappeared.

Am I right? Should I put extra logic for that? (like stop and restart timer?) (or declare variable to skip in it?)

Thanks.

Upvotes: 1

Views: 196

Answers (1)

Ahmad ElMadi
Ahmad ElMadi

Reputation: 2617

In Andriod usually when you are in Page1 and then you navigated to Page2, although Page1 is hidden it is not killed therefore the worker threads would still working (in that case it is the timer) My suggestion is to override the OnDisappearing method in the code behind of Page1 and place the code that would make the timer stop or being ignored. If it is for me , I would also place a boolean flag and called _isPageShown so when you override OnDisappearing and OnAppearing you just put the value of _isPageShown to false or true accordingly. Then in the callback of the timer you check the flag if it is true or false and act accordingly.

Upvotes: 2

Related Questions