Since you say that you need to make a request every minute after the page is displayed, a good solution is to start the timer in the ViewWillAppear() method and stop it in ViewWillDisappear() - it will only start when the ViewController is visible in the foreground. Unpinning OnTimedEvent is important to avoid memory leaks.
Is this what you need, or do you have more specific requirements?
Code example:
class MyViewController : UIViewController { public MyViewController(IntPtr handle) : base(handle) { } private Timer timer; private bool timerEventBinded; public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); if (timer == null) { timer = new Timer(); timer.Enabled = true; timer.Interval = 60000; } if (!timerEventBinded) { timer.Elapsed += OnTimedEvent; timerEventBinded = true; } timer.Start(); } public override void ViewWillDisappear(bool animated) { if (timer != null) { timer.Stop(); if (timerEventBinded) { timer.Elapsed -= OnTimedEvent; timerEventBinded = false; } timer = null; } base.ViewWillDisappear(animated); } private void OnTimedEvent(Object src, ElapsedEventArgs e) {
source share