Handle backgrounding properly with expiration handler

This commit is contained in:
Amy Burns 2017-04-11 17:45:12 -04:00
Родитель 70d3660d8e
Коммит 798830e55a
4 изменённых файлов: 20 добавлений и 11 удалений

Просмотреть файл

@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using System.Threading.Tasks;
@ -57,11 +57,15 @@ namespace BackgroundExecution
{
Console.WriteLine ("App entering background state.");
nint taskID = 0;
// if you're creating a VOIP application, this is how you set the keep alive
//UIApplication.SharedApplication.SetKeepAliveTimout(600, () => { /* keep alive handler code*/ });
// register a long running task, and then start it on a new thread so that this method can return
var taskID = UIApplication.SharedApplication.BeginBackgroundTask (null);
taskID = UIApplication.SharedApplication.BeginBackgroundTask (()=>{
Console.WriteLine("Running out of time to complete you background task!");
UIApplication.SharedApplication.EndBackgroundTask(taskID);
});
Task.Factory.StartNew (() => FinishLongRunningTask (taskID));
}

Просмотреть файл

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -11,6 +11,7 @@ namespace BackgroundExecution
{
public partial class HomeScreen : UIViewController
{
#region Constructors
// The IntPtr and initWithCoder constructors are required for items that need
@ -44,8 +45,13 @@ namespace BackgroundExecution
void DoSomething ()
{
nint taskID = 0;
// register our background task
var taskID = UIApplication.SharedApplication.BeginBackgroundTask (BackgroundTaskExpiring);
taskID = UIApplication.SharedApplication.BeginBackgroundTask(() =>
{
Console.WriteLine("Running out of time to complete you background task!");
UIApplication.SharedApplication.EndBackgroundTask(taskID);
});
Console.WriteLine ("Starting background task {0}", taskID);
@ -58,10 +64,6 @@ namespace BackgroundExecution
UIApplication.SharedApplication.EndBackgroundTask (taskID);
}
public void BackgroundTaskExpiring ()
{
Console.WriteLine ("Running out of time to complete you background task!");
}
}
}

Просмотреть файл

@ -1,4 +1,4 @@
using System;
using System;
using AVFoundation;
using Foundation;
using System.Collections.Generic;
@ -173,6 +173,7 @@ namespace QRchestra
{
pipelineRunningTask = UIApplication.SharedApplication.BeginBackgroundTask (delegate {
Console.WriteLine ("Video capture pipeline background task expired");
UIApplication.SharedApplication.EndBackgroundTask(pipelineRunningTask);
});
}

Просмотреть файл

@ -1,4 +1,4 @@
using System;
using System;
using CoreGraphics;
using Foundation;
@ -167,7 +167,9 @@ namespace RosyWriter
// Make sure we have time to finish saving the movie if the app is backgrounded during recording
if (UIDevice.CurrentDevice.IsMultitaskingSupported)
// HACK: Cast nint to int
backgroundRecordingID = (int)UIApplication.SharedApplication.BeginBackgroundTask (() => {});
backgroundRecordingID = (int)UIApplication.SharedApplication.BeginBackgroundTask (() => {
UIApplication.SharedApplication.EndBackgroundTask(backgroundRecordingID);
});
});
}