I have a CreateSchedule() function which execute when I open app.
private async static Task<int> CreateSchedule()
{
try
{
DateTime now = DateTime.Now;
DateTime planTill = now.AddMinutes(15);
do
{
await DoSomeWork();
now = DateTime.Now;
await Task.Delay(TimeSpan.FromSeconds(5));
} while (now < planTill);
return 1;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return 0;
}
}
This function runs continuously (I have used do-while loop) when the app is in active state or in foreground state. But when I minimize or press Back or Start button on WP, this function won't execute anymore.
So is there any ways to execute that function continuously even when the app is minimized or Start or Back button is pressed.
I have tried using BackgroundTask but that seems to execute only between 0-15 minutes from the time of registration which is not my requirement.
Please suggest some if there are any Threads we can run in background in Windows Phone.
Thanks in advance!
It is by design that you can't run code in the background. The only option is a BackgroundTask
which has many limitations (must be on lock screen, every 15 minutes etc.) as you already stated.
So the short answer is you can't.
You can read more about background tasks here.