Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run background task every X amount of time

I would like to start a service that once in awhile on all platforms has checked is there a notification to appear or not. Is there any nuget to connect all platforms or some examples?

like image 858
Maury Avatar asked Jun 19 '15 12:06

Maury


1 Answers

You can use the Device.StartTimer(TimeSpan minutes) method to start a background task that will repeat after the given time span. Here is a code example:

var minutes = TimeSpan.FromMinutes (3); 

Device.StartTimer (minutes, () => {

    // call your method to check for notifications here

    // Returning true means you want to repeat this timer
    return true;
});

This is included with Xamarin Forms, so you don't need any platform specific logic.

http://iosapi.xamarin.com/index.aspx?link=M%3AXamarin.Forms.Device.StartTimer(System.TimeSpan%2CSystem.Func%7BSystem.Boolean%7D)

like image 152
pnavk Avatar answered Nov 20 '22 14:11

pnavk