1

I want to use System.Timer in my Xamarin Forms based project. I am actually converting my Xamarin iOS project to Xamarin Formbased project. My Xamarin iOS project has all code for Timer using System.Timer

  aTimer = new Timer (tm);

  // Hook up the Elapsed event for the timer.
  aTimer.Elapsed += new ElapsedEventHandler (OnTimedEvent);


  aTimer.Enabled = true;
  aTimer.Start ();

When i try to use the same code in Xamarin Forms project it is giving error. First of all

using System.Timers;

It says: The type or namespace name Timers does not exist in the namespace System. Are you missing a assembly reference?

Does Xamarin iOS System different than Xamarin Forms System reference?

User382
  • 864
  • 19
  • 42

2 Answers2

7

PCL projects do not support System.Timer.

Xamarin Forms has a built in Timer to help workaround this limitation

Device.StartTimer (new TimeSpan (0, 0, 60), () => {
    // do something every 60 seconds
    return true; // runs again, or false to stop
});

If you want to start and stop the timer via buttons, you could do something like this:

bool timerStatus = false;

btnStart.Clicked += delegate {
  timerStatus = true;
  Device.StartTimer (new TimeSpan(h,m,x), () => {
     if (timerStatus) {
       // do work
     }
     return timerStatus;
  });
};

btnStop.Clicked += delegate {
  timerStatus = false;
};
Jason
  • 86,222
  • 15
  • 131
  • 146
  • I have two buttons 1 - Start timer and 2 - Stop timer. Considering above code, how can i start and stop timer on button clicks. In above code it has new TimeSpan (0, 0, 60) which allocates new instance. How do i return FALSE on STOP button click. – User382 May 28 '15 at 15:30
  • Useful links: [Xamarin Forms Timer](https://xamarinhelp.com/xamarin-forms-timer/), [Device.StartTimer async](https://forums.xamarin.com/discussion/comment/100283/#Comment_100283) – testing Apr 26 '19 at 11:14
0

Xamarin Forms libraries are Portable class libraries and as such, Timers are not part of the API for certain target platform combinations.

A good implementation replacement for a time would be an implementation using Task.Delay, marked as internal to avoid issues when using the PCL library on a platform that has timers available. You can use this code as a drop-in shim (source: link above):

internal delegate void TimerCallback(object state);

internal sealed class Timer : CancellationTokenSource, IDisposable
{
    internal Timer(TimerCallback callback, object state, int dueTime, int period)
    {
        Contract.Assert(period == -1, "This stub implementation only supports dueTime.");
        Task.Delay(dueTime, Token).ContinueWith((t, s) =>
        {
            var tuple = (Tuple<TimerCallback, object>)s;
            tuple.Item1(tuple.Item2);
        }, Tuple.Create(callback, state), CancellationToken.None,
            TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion,
            TaskScheduler.Default);
    }

    public new void Dispose() { base.Cancel(); }
}
Community
  • 1
  • 1
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172