Share via


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

Definition

Caution

Use BindableObject.Dispatcher.StartTimer() or BindableObject.Dispatcher.DispatchDelayed() instead.

Starts a recurring timer using the device clock capabilities.

public:
 static void StartTimer(TimeSpan interval, Func<bool> ^ callback);
[System.Obsolete("Use BindableObject.Dispatcher.StartTimer() or BindableObject.Dispatcher.DispatchDelayed() instead.")]
public static void StartTimer (TimeSpan interval, Func<bool> callback);
[<System.Obsolete("Use BindableObject.Dispatcher.StartTimer() or BindableObject.Dispatcher.DispatchDelayed() instead.")>]
static member StartTimer : TimeSpan * Func<bool> -> unit
Public Shared Sub StartTimer (interval As TimeSpan, callback As Func(Of Boolean))

Parameters

interval
TimeSpan

The interval between invocations of the callback.

callback
Func<Boolean>

The action to run when the timer elapses.

Attributes

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