Device.StartTimer(TimeSpan, Func<Boolean>) Method

Definition

Starts a recurring timer using the device clock capabilities.

public static void StartTimer (TimeSpan interval, Func<bool> callback);
static member StartTimer : TimeSpan * Func<bool> -> unit

Parameters

interval
TimeSpan

The interval between invocations of the callback.

callback
Func<Boolean>

The action to run when the timer elapses.

Remarks

While the callback returns true, the timer will keep recurring.

If you want the code inside the timer to interact on the UI thread (e.g. setting text of a Label or showing an alert), it should be done within a BeginInvokeOnMainThread expression, which will be nested inside the timer (see below).

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

Applies to