5

What's a benefits or drawbacks of using Device.StartTimer vs System.Threading.Timer?

Both are firing on background threads, both are cross-platform and netstandard2 compatible. System.Threading.Timer has a bonus points of being non-Xamarin specific.

What should I use and when?

  • Device.StartTimer use native APIs.
  • According to github.com/mono, System.Threading.Timer seems to use dedicated thread for all timers. Am I right, or do Xamarin use another implementation?
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Leotsarev
  • 1,040
  • 7
  • 23
  • 2
    Also, there is good, upvoted and very useful question about how to select between `System.Threading.Timer` and `System.Timers.Timer` https://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer – Leotsarev Oct 18 '19 at 07:26

2 Answers2

8

Official answer from Xamarin documentation team: https://github.com/MicrosoftDocs/xamarin-docs/issues/2243#issuecomment-543608668

Use either.

Device.StartTimer was implemented long before .NET Standard came along, and in those days the Timer class was unavailable to PCL projects. Now that the Timer class is available, there's no advantage/need to use Device.StartTimer. But that API won't vanish, because there will be older projects that still rely on it.

Community
  • 1
  • 1
Leotsarev
  • 1,040
  • 7
  • 23
1

What's a benefits or drawbacks of using Device.StartTimer vs System.Threading.Timer?

Device.StartTimer use native apis. So you should invoke it prior because it starts a recurring timer on the UI thread using the device clock capabilities.

Device.StartTimer(TimeSpan.FromSeconds(30), () =>
{
    // Do something
   
    return true; // True = Repeat again, False = Stop the timer
});

And in specific platforms ,it will do the following .

iOS

public void StartTimer(TimeSpan interval, Func<bool> callback)
{
    NSTimer timer = NSTimer.CreateRepeatingTimer(interval, t =>
    {
        if (!callback())
            t.Invalidate();
    });
    NSRunLoop.Main.AddTimer(timer, NSRunLoopMode.Common);
}

Android

public void StartTimer(TimeSpan interval, Func<bool> callback)
{
    var handler = new Handler(Looper.MainLooper);
    handler.PostDelayed(() =>
    {
        if (callback())
            StartTimer(interval, callback);

        handler.Dispose();
        handler = null;
    }, (long)interval.TotalMilliseconds);
}
Community
  • 1
  • 1
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • 1
    Docs says otherwise : >If the code inside the timer interacts with the user-interface (such as setting the text of a Label or displaying an alert) it should be done inside a BeginInvokeOnMainThread expression (see below). https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/device – Leotsarev Oct 18 '19 at 08:12
  • Yes, my code is just show the basic usage of timer . – Lucas Zhang Oct 18 '19 at 08:14
  • What I'm trying to say is you can use `Device.StartTimer` firstly . – Lucas Zhang Oct 18 '19 at 08:17
  • *"So you should invoke it prior "* - I have no idea what you are trying to say here. Likewise with your comment "you can use Device.StartTimer firstly". "prior"? "firstly"? Those imply BEFORE doing something else. What is the "something else" that you "can use Device.StartTimer" before? I don't understand what your answer is saying. – ToolmakerSteve Nov 11 '21 at 01:46