從 Windows 10 開始,用戶無須再將你的應用添加到鎖屏界面,便可利用後臺任務,通用 Windows 應用必須在註冊任何後臺觸發器類型以前調用 RequestAccessAsync:windows
await BackgroundExecutionManager.RequestAccessAsync();
因爲對於內存較低的設備的資源約束,後臺任務可能具備內存限制,該限制決定了後臺任務可使用的內存上限markdown
在內存受限的設備上,對於能夠安裝在一臺設備上並在任何給定時間使用後臺任務的應用數量有所限制。 若是超出該數量,將沒法調用註冊全部後臺任務時所需的 RequestAccessAsyncapp
若是後臺任務嘗試執行超過此限制的操做,則該操做將失敗,並可能生成內存不足的異常狀況,但該任務能夠處理此狀況。 若是該任務沒法處理內存不足的異常狀況,或者因爲嘗試的操做的性質致使沒法生成內存不足的異常狀況,任務將當即終止less
你可使用 MemoryManager PAI 查詢當前內存使用量和限制,以發現上限(如有),並監視正在進行的後臺任務內存使用量異步
對於進程內後臺活動,不要設置 TaskEntryPoint.。將其保留爲空可啓用默認入口點,這是 Application 對象上稱爲 OnBackgroundActivated() 的一種新的受保護方法socket
除非你豁免你的應用,以便它能夠在節電模式打開時仍能夠運行後臺任務和接收推送通知,不然當節電模式功能啓用時,若是設備未鏈接到外部電源且電量低於指定剩餘電量,它將阻止後臺任務運行。 這不會阻止你註冊後臺任務async
後臺任務受其基於觸發器類型獲取的時鐘時間使用的限制。 ide
大多數觸發器限制爲 30 秒的時鐘時間使用,若是後臺活動的運行時間超過執行時間限制,即便在應用的前臺進程內運行,後臺活動也會終止(進程內後臺任務)。而另外一些觸發器在完成耗時任務時最多能夠運行 10 分鐘。 爲了延長電池使用時間併爲前臺應用提供最佳用戶體驗,後臺任務應該是輕量級任務函數
Reason: Windows Store apps have their threads suspended when not in the forground.
Benefit: This prevents background apps fom consuming battery power and prevents apps the user is not interacting with from affecting the responsiveness of the foreground app.性能
System can update your tile or show a toast notification even when your app is suspended or terminated. —- Toast and Notification
Your app could transfer large files even when in the background. — Networking
All using background task. Windows Store apps can execute code when not in the foreground by using background task.
Implement the code you want executed as a background task.
Decide what triggers your background task code (eg: timer , user log in , ..)
Add a manifest declaration so that Windows knows how to active background task code.
只是決定什麼狀況下觸發,從而運行
Register your app’s background tasks with Windows the first time your app is activated.
注意:
第一次須要在代碼中註冊
此外,須要在項目中添加對 WinRT 的引用,reference
When a trigger occurs, Windows creates a new process, loads your background task code into it, and calls an entry point. The background task process runs in the package’s app container. (background task typically execute in a different process than the app’s process)
注:Windows Store apps runs in a different security context than a desktop apps. The security context is called app container, and it restricts which resources a Windows Store app can access.
a new process —- 獨立進程 (backgroundtaskhost.exe)
須要說明,通常background task 運行在哪一個進程,由manifest – declarations – background task – Executable 決定,文檔上:
For most of the triggers, you must leave this blank, Which tells Windows to use its own BackgroundTaskHost.exe process.(系統的BackgroundTaskHost.exe, 任務管理器只看見一個)
Background is not allowed to update the app’s user interface, but can update tiles and badges, or cause the display of a toast notification
重點:
The background task process runs in the package’s app container. This means that the manifest’s package capabilities and app declarations do apply to the background task process. The background task process can write to the package’s data setting and folders. (for an app and its background tasks to communicate with each other). One process can signal when data is ready by calling ApplicationData’s SingalDataChanged method.
Your app must run at least once in order to register a background task and its desired trigger with Windows.(第一次在代碼中註冊)
Once registered, your app can be running, suspended, or even terminated. When the trigger occurs, Windows starts your app’s background task.
//在註冊前運行便可
await Windows.ApplicationModel.Background.BackgroundExecutionManager.RequestAccessAsync();
Background task is the only way to execute code when a PC is in Connected Standby mode.
有兩種方法能夠實現後臺任務:
進程外後臺任務實現爲操做系統在獨立進程 (backgroundtaskhost.exe) 中運行的輕型類。 進程外後臺任務編寫來實現 IBackgroundTask 接口的類。 經過使用 BackgroundTaskBuilder 類註冊後臺任務。 註冊後臺任務時,類名稱將用於指定入口點
後臺任務受其基於觸發器類型獲取的時鐘時間使用的限制。 大多數觸發器限制爲 30 秒的時鐘時間使用,而另外一些觸發器在完成耗時任務時最多能夠運行 10 分鐘。 爲了延長電池使用時間併爲前臺應用提供最佳用戶體驗,後臺任務應該是輕量級任務。
若是後臺任務類運行異步代碼,則確保使用延遲。 不然,當使用 Run 方法(或針對進程內後臺任務使用 OnBackgroundActivated 方法)時,你的後臺任務可能會提早終止
BackgroundTaskDeferral _deferral; // Note: defined at class scope so we can mark it complete inside the OnCancel() callback if we choose to support cancellation
public async void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral()
//
// TODO: Insert code to start one or more asynchronous methods using the
// await keyword, for example:
//
// await ExampleMethodAsync();
//
_deferral.Complete();
}
若是你的應用將更新,請建立和註冊一個 ServicingComplete 後臺任務,以便取消註冊適用於前一版本應用的後臺任務,並註冊適用於新版本應用的後臺任務。 此時也很是適合在前臺運行的上下文以外執行所需的應用更新
建議每一個 app 均可以裝一個,它能夠在 你沒運行以前作一些操做
namespace MyApp.BackgroundTasks
{ // NOTE: determines filename "MyApp.BackgroundTasks.WinMD"
using Windows.ApplicationModel.Background; // For IBackgroundTask & IBackgroundTaskInstance
using Windows.Storage;
// NOTE: WinRT components MUST be public and sealed
public sealed class MyBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{ // Register cancelation handler (see the "Background task cancellation" section)
// NOTE: Once canceled, a task has 5 seconds to complete or the process is killed
taskInstance.Canceled += (IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) =>
{
// TODO: Tell task it should cancel itself as soon as possible...
};
// Recommended: Adjust task behavior based on CPU and network availability
// For example: A mail app could download mail for all folders when cost is
// low and only download mail for the Inbox folder when cost is high
switch (BackgroundWorkCost.CurrentBackgroundWorkCost)
{
case BackgroundWorkCostValue.Low: // The task can use CPU & network
case BackgroundWorkCostValue.Medium: // The task can use some CPU & network
case BackgroundWorkCostValue.High: // The task should avoid using CPU & network
// This example records the last trigger time in an application data setting
// so the app can read it later if it chooses. We do regardless of work cost.
ApplicationData.Current.LocalSettings.Values["LastTriggerTime"] = DateTimeOffset.Now;
break;
}
}
}
}
Must implemented as a WinRT comment in VS. This project simply creates a dynamic-link library file. The file extension is .WinMD instead of .DLL .
When you register the full name of this class. The windows will be ready to execute this task ,it will try to load a .WinMD file whose name matches the namespace.
加載這文件時候,會先加載構造函數,不寫系統默認一個空構造,你寫了就不去加載這個空的構造函數。(包含run)
經過在 BackgroundTaskRegistration.AllTasks
屬性中迭代,查明後臺任務是否已註冊。 此步驟很是重要;若是應用不檢查現有後臺任務註冊,則它可能會輕鬆屢次註冊該任務,這會致使性能問題和工做結束前超出任務的最大可用 CPU 時間
var taskRegistered = false;
var exampleTaskName = "ExampleBackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == exampleTaskName)
{
taskRegistered = true;
break;
}
}
若是後臺任務還沒有註冊,則使用 BackgroundTaskBuilder 建立你的後臺任務的一個實例。 任務入口點應爲命名空間爲前綴的後臺任務的名稱。
後臺任務觸發器控制後臺任務什麼時候運行。 有關可能的觸發器的列表,請參閱 SystemTrigger。
例如,此代碼建立一個新後臺任務並將其設置爲在 TimeZoneChanged 觸發器引起時運行:
var builder = new BackgroundTaskBuilder();
builder.Name = exampleTaskName;
builder.TaskEntryPoint = "RuntimeComponent1.ExampleBackgroundTask";
builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
(可選)在觸發器事件發生後,你能夠添加條件控制任務什麼時候運行。 例如,若是你不但願在用戶存在前運行任務,請使用條件 UserPresent。 有關可能條件的列表,請參閱 SystemConditionType
builder.AddCondition(new SystemCondition(SystemConditionType.UserPresent));
經過在 BackgroundTaskBuilder 對象上調用 Register 方法來註冊後臺任務。 存儲 BackgroundTaskRegistration 結果,以即可以在下一步中使用該結果
BackgroundTaskRegistration task = builder.Register();
通用 Windows 應用必須在註冊任何後臺觸發器類型以前調用 RequestAccessAsync:
await BackgroundExecutionManager.RequestAccessAsync();
你應該使用 BackgroundTaskCompletedEventHandler 註冊一個方法,以便應用能夠從後臺任務中獲取結果。 當啓動或恢復應用時,若是自從上次在應用前臺運行後後臺任務就已完成,將調用標記方法。 (若是應用當前位於前臺時後臺任務完成,將當即調用 OnCompleted 方法)
private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
var key = task.TaskId.ToString();
var message = settings.Values[key].ToString();
UpdateUI(message);
}
回到已註冊後臺任務的位置。 在該代碼行以後,添加一個新的 BackgroundTaskCompletedEventHandler 對象。 提供 OnCompleted 方法做爲 BackgroundTaskCompletedEventHandler 構造函數的參數:
task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
略^^
The meaning of Executable field, this tells Windows which process to execute when the trigger fires. (!!!)
This process will load your WinRT component’s .WinMD file and execute your task’s code. There are two options for this field. For most of the triggers, you must leave this field blank, which tells Windows to use its own Background TaskHost.exe process. (Windows or App container?)
註釋:Windows Store apps run in a different security context than desktop apps. This security context is called an app container (discussed in the appendix, 「App containers」), and it restricts which resources a Windows Store app can access.
For a PushNotificationTrigger, you can leave this field blank or you can specify(指定) the name of your own app’s executable. If you use the latter, Windows will have your app’s process load the WinRT component and run the task in the same process as your app.
This is not the recommended thing to do, but it allows your background task’s code the ability to access the same state (memory) as your app. However, if your app is suspended, all threads but the thread running the background task code remain suspended, so you must not perform any interthread communication or deadlocks will occur. In addition, because the GUI thread remains suspended, the background task cannot update the app’s user interface. If the app’s process is not running, Windows will activate it, but the app is not launched with a main view or hosted view activation. The result of all this is that your background task cannot have any expectations of the app’s state and, in fact, the app might not have its state fully initialized.
For a ControlChannelTrigger, you must not leave the Executable field blank; instead, you must specify your app’s executable name and your WinRT component’s .WinMD file must load in the app’s process.
As mentioned previously, the ControlChannelTrigger is used for RTC apps, and these apps typically have a socket open in the background task. For the app to respond to the incoming call on the socket, the background task and the app have to share the same process. Everything I said earlier still holds true in this scenario too; that is, the app will not be fully initialized and you should avoid interthread communication.
For the declaration’s Entry Point field, enter the full name (including the namespace) of the WinRT class you created in step 1 (for example, MyApp.BackgroundTasks.MyBackgroundTask). This tells the host process the name of the .WinMD file to load (MyApp.BackgroundTasks.WinMD) and the name of the class to construct in order to call its Run method.
Lock-Screen allowing that app’s background tasks to consume system resources even when the PC is on battery power. These apps typically have a real-time networking requirement like a chat or VoIP application
最多顯示七個,能夠本身去系統裏面設置(你的app得支持lock-screen 才行)