Edit

Share via


UIApplication.BeginBackgroundTask Method

Definition

Overloads

BeginBackgroundTask(String, Action)

Requests that the app be allowed to process in the background.

BeginBackgroundTask(Action)

Indicates the inception of a new long-running background task.

BeginBackgroundTask(String, Action)

Requests that the app be allowed to process in the background.

[Foundation.Advice("Overriding this method requires a call to the overriden method.")]
[Foundation.Export("beginBackgroundTaskWithName:expirationHandler:")]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.iOS, 7, 0, ObjCRuntime.PlatformArchitecture.All, null)]
[ObjCRuntime.RequiresSuper]
[ObjCRuntime.ThreadSafe]
public virtual nint BeginBackgroundTask (string taskName, Action expirationHandler);
abstract member BeginBackgroundTask : string * Action -> nint
override this.BeginBackgroundTask : string * Action -> nint

Parameters

taskName
String

A name for the task (useful for debugging).

expirationHandler
Action

Called shortly before the allowed background time for the app expires.

Returns

System.System.IntPtr System.nativeint

An id that, when passed to EndBackgroundTask(nint), indicates that background processing has ended.

Attributes

Remarks

This method, with EndBackgroundTask(nint), bookends code that should be allowed to continue running in the background when another app is in the foreground. The returned int should be passed to EndBackgroundTask(nint) at the appropriate time.

This method does not cause the app to enter a background state or launch asynchronous code, despite its name. Rather, it requests that the system make background processing available.

This method may be called multiple times and may be called from background threads. Background processing will continue either until the allowed background time (generally 3 or 10 minutes total) expires or until EndBackgroundTask(nint) has been called for all started tasks.

The expirationHandler is only called if allowed background time is close to expiring (3-4 seconds).

EndBackgroundTask(nint) must be called within the expiration handler, as well as in the normal course of execution.

Task.Factory.StartNew( () => {
    //expirationHandler only called if background time allowed exceeded
    var taskId = UIApplication.SharedApplication.BeginBackgroundTask("bgTask", () => {
        Console.WriteLine("Exhausted time");
        UIApplication.SharedApplication.EndBackgroundTask(taskId);        
    });
    while(myFlag == true)
    {
        Console.WriteLine(UIApplication.SharedApplication.TimeRemaining);
        myFlag = SomeCalculationNeedsMoreTime();
    }
    //Only called if loop terminated due to myFlag and not expiration of time
    UIApplication.SharedApplication.EndBackgroundTask(taskId);
});              

This can be used from a background thread.

Applies to

BeginBackgroundTask(Action)

Indicates the inception of a new long-running background task.

[Foundation.Advice("Overriding this method requires a call to the overriden method.")]
[Foundation.Export("beginBackgroundTaskWithExpirationHandler:")]
[ObjCRuntime.RequiresSuper]
[ObjCRuntime.ThreadSafe]
public virtual nint BeginBackgroundTask (Action backgroundTimeExpired);
abstract member BeginBackgroundTask : Action -> nint
override this.BeginBackgroundTask : Action -> nint

Parameters

backgroundTimeExpired
Action

Action that runs when background time expires.

This parameter can be null.

Returns

System.System.IntPtr System.nativeint

An id that, when passed to EndBackgroundTask(nint), indicates that background processing has ended.

Attributes

Remarks

This method, with EndBackgroundTask(nint), bookends code that should be allowed to continue running in the background when another app is in the foreground. The returned int should be passed to EndBackgroundTask(nint) at the appropriate time.

This method does not cause the app to enter a background state or launch asynchronous code, despite its name. Rather, it requests that the system make background processing available.

This method may be called multiple times and may be called from background threads. Background processing will continue either until the allowed background time (generally 3 or 10 minutes total) expires or until EndBackgroundTask(nint) has been called for all started tasks.

The expirationHandler is only called if allowed background time is close to expiring (3-4 seconds).

EndBackgroundTask(nint) must be called within the expiration handler, as well as in the normal course of execution.

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.TimeRemaining);
        myFlag = SomeCalculationNeedsMoreTime();
    }
    //Only called if loop terminated due to myFlag and not expiration of time
    UIApplication.SharedApplication.EndBackgroundTask(taskId);
});              

This can be used from a background thread.

Applies to