Scott Nimrod
Scott Nimrod

Reputation: 11595

Does Xamarin.Forms support periodic background tasks?

I am having a difficult time finding documentation on background tasks support for Xamarin.Forms. Does Xamarin.Forms provide support for periodic background tasks?

I need to implement this for both Windows Phone 10 and Android.

Upvotes: 10

Views: 12904

Answers (3)

NPV
NPV

Reputation: 41

I use Xamarin.Forms.Device.StartTimer Method, it starts a recurring timer using the device clock capabilities. While the callback returns true, the timer will keep recurring.

http://developer.xamarin.com/api/member/Xamarin.Forms.Device.StartTimer/

Upvotes: -5

Majkl
Majkl

Reputation: 763

Yeas, but it depends what you need to do.

You can for example use System.Threading.Timer (.net class) is Activity/Service

private System.Threading.Timer timer;

In Activity OnCreate

TimeSpan timerTime = new TimeSpan(0, 0, 0, 0, 1000);
            timer = new System.Threading.Timer(new System.Threading.TimerCallback(OnTimerFired), null, timerTime, timerTime);

In Activity OnDestroy

if (timer != null)
            {
                timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
                timer.Dispose();
                timer = null;
            }


private void OnTimerFired(object state)
{
    Do some periodic job
}

Upvotes: -5

Related Questions