I'm trying this code from Xamarin. Ios documentation for background tasks: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks. It says that the variable taskId in line 6 has CS0841: Cannot use local variable 'taskId' before it is declared. How should I assign the taskId? Also, how do you declare myFlag? I cannot find any descriptions of how to use this variable.
Task.Factory.StartNew( () => {
//expirationHandler only called if background time allowed exceeded
var taskId = UIApplication.SharedApplication.BeginBackgroundTask(() => {
Console.WriteLine("Exhausted time");
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});
while(myFlag == true)
{
Console.WriteLine(UIApplication.SharedApplication.BackgroundTimeRemaining);
myFlag = SomeCalculationNeedsMoreTime();
}
//Only called if loop terminated due to myFlag and not the expiration of time
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});