參考文獻:
1. 31 days of Windows 8#12 Background Task:
http://www.jeffblankenburg.com/2012/11/12/31-days-of-windows-8-day-12-background-tasks/
2. 31 days of Windows 8 #11 Lock Screen :
http://www.jeffblankenburg.com/2012/11/11/31-days-of-windows-8-day-11-lock-screen-apps/
3. Background Task 白皮書:
http://www.microsoft.com/en-us/download/details.aspx?id=27411在使用鎖屏應用的時候,你要保證你明白自己要做些什么準備:
1. 使用Wide Logo,即你要提供wide logo 310*150
2. 使用Badge, 即你要提供badge logo 24*24
3. 使用Background Task, 而且background task 支持的事件是Control channel, timer, push notification類型。
4. 設置你的Background Task的EntryPoint
5. 你需要在Manifest里面聲明你使用LockScreen。
具體的操作步驟大家可以參考文獻2。
鎖屏應用比較簡單,重要的一點是,如果你在程序中詢問了用戶是否允許使用鎖屏應用,你將只有這一次修改的機會,不然你就只能通過PC Control進行設置了。但是如果你的Windows 8 沒有激活的話,你只能卸載應用程序,重新安裝一遍了。
代碼如下:
1 //要先請求允許,如果允許的話,你才能更新Badge,如果沒有這一步,Badge將 不會顯示在LockScreen上面
2 create_task(BackgroundExecutionManager::RequestAccessAsync()).then([this](BackgroundAccessStatus status)
3 {
4 //這一步可以不要
5 //BackgroundAccessStatus status1 = BackgroundExecutionManager::GetAccessStatus();
6 if((status == BackgroundAccessStatus::AllowedWithAlwaysOnRealTimeConnectivity)||
7 (status == BackgroundAccessStatus::AllowedMayUseActiveRealTimeConnectivity))
8 {
9 XmlDocument^ badgeData = BadgeUpdateManager::GetTemplateContent(BadgeTemplateType::BadgeNumber);
10 XmlNodeList^ badgeXML = badgeData->GetElementsByTagName("badge");
11 ((XmlElement^)badgeXML->Item(0))->SetAttribute("value","Playing");
12
13 BadgeNotification^ badge = ref new BadgeNotification(badgeData);
14 BadgeUpdateManager::CreateBadgeUpdaterForApplication()->Update(badge);
15 }
16 });
運行,然后會彈出一個對話框,像這樣

點擊Allow之后,按下WIN+L你就可以看到鎖屏上面出現的Badge圖片和你的number了。
一般情況下,我們會使用后臺任務(Background Task)來更新Lock Screen App的數據。
后臺任務
1. 什么是后臺任務? 后臺任務是:即使程序已經被掛起或者不在運行了,還在默默地執行的任務。
2.
后臺任務與前臺任務的區別? 首先,前臺任務占據了整個屏幕,用戶直接與其進行交互。后臺任務不能與用戶交互,除了(Tile, Toast, 和Lock Screen)
其次,因為前臺要與用戶交互,它使用所有可用的系統資源,包括CPU time 和Network資源,并且不受限。后臺任務使用系統資源的時候是受限制的。
再次,前臺任務處理主要事務,后臺任務處理短時間、輕量級的事務。
最后,后臺任務不論前臺任務是否處于Running狀態,它都會運行。
3. 后臺任務的目的? 大家都知道,Windows 8 應用程序的生命周期分為Running,Suspended,Terminated三種狀態。App處于前臺時,為Running狀態,處于后臺時,為Suspended狀態,用戶關閉App時或者在Suspended狀態太久,系統自動關閉App時,為Terminated狀態。
我們可以從Suspended狀態將App變為Running狀態,我們不能在后臺運行太復雜,太耗時耗資源的程序,因為如果你這么做了,你將會非常耗費電量,并且,用戶切換回前臺時,會感覺到非常卡,有延遲。
因為上述的目的:延長電池用量,保證用戶流暢的體驗,我們需要限制后臺任務對資源的使用,而且我們的后臺任務要盡量精簡。
4. 后臺任務的執行環境? 一般情況下,我們都要把我們的后臺任務作為一個Runtime Component,引用到主工程中去。這樣,一個后臺任務就是一個class library,一個in-proc server DLL。這個library可以在我們的App中運行,也可以在系統提供的主機環境中運行(BackgroundTaskHost.exe)。這個exe是在App相同的容器內運行,當它不需要的時候,會自動退出。
5. 后臺任務的適合場景?
播放音樂,上傳下載文件,刷新瓷貼、通知、LockScreen,應用程序間共享合約。下載Mail,VOIP、IM信息,用戶改變系統設置
6. 后臺任務基本概念?
Background task 一個實現了IBackgroundTask接口的類
A class or JavaScript page implemented by the app to provide functionality even if the app is not in the foreground.
Background trigger 一系列事件,每個后臺任務都需要至少一個Trigger
A system-defined event that an app can associate with a background task. When the trigger is fired by the system, an app background task that is associated with the trigger is launched.
Background condition 一些必須滿足的條件,可以有,也可以沒有condition
A set of zero or more conditions that need to be satisfied before the background task can run.
BackgroundTaskHost.exe 一個裝載后臺任務的容器
A system-provided host executable to run the background task.
EntryPoint 一個實現了IBackgroundTask接口的類的名字
The name of the C# or C++ class that implements the background task.
Executable
The name of the executable that hosts the background task class.
Foreground app
The app that the user is actively interacting with.
Lock screen Win+L就可以看到你的Lock Screen了
This is the first screen shown following a Windows 8 boot, resuming from sleep, or locking your PC. It presents a user-customizable surface that both conveys information and protects against accidental logon attempts.
Start page
The name of the JavaScript page that implements the background task.
7. 后臺任務的運行原理?
后臺任務的注冊,運行,調試,請參閱參考文獻1,2,3.這里主要介紹一下后臺任務的運行原理。大家可以參考下圖進行理解

虛線兩邊分別表示App和System,App就是我們的應用程序,System就是負責處理后臺任務的Service。
首先,我們要注冊Trigger,Trigger有多種,大家可以參考文獻3。
其次,在應用程序中注冊后臺任務(包含了什么樣的Trigger可以觸發這個后臺任務),注冊之后,在System Infra中就永久保留了這個注冊信息。不論你是否關閉了應用程序還是重新啟動了電腦,這個注冊信息都會存在。
再次,當合適的Trigger事件來臨,System Infra 會搜索與這個Trigger相匹配的后臺任務
最后,啟動該后臺任務。
我們可以在任務管理器中找到Background Task Infrastracture Service :

右鍵點擊,Open Service,可以看到具體的描述:

可以看到,這個服務就是控制哪個后臺任務可以在系統中運行,也就是我們圖中的System Infra。
8 App狀態與后臺任務的關系 ?
看到上面的過程,大家或許會疑問,如果Trigger事件來臨,App已經關閉,BackgroundTask還會執行么?答案是:一定會,不論你的App是Running還是Suspended或者是Terminated狀態。
但是,還記得我們說過的,我們的后臺任務可以運行于App中,也可以運行于系統提供的環境中么?
如果運行于系統提供的環境中,那么,答案同上,而且App保持原來的狀態。例如:App是Terminate狀態,后臺任務運行,App不會啟動,依舊是Terminate狀態。
如果運行于App中,那么答案同上,但是App的狀態會略有不同,這種不同只存在Terminate狀態,如果App是Terminate狀態的話,Trigger來臨,App會被啟動,但是其UI線程不會被啟動,后臺任務啟動。也就是說App雖然啟動了,但是不會將App帶回前臺。
也就是說,無論如何,當Trigger事件被觸發,后臺任務的啟動是由系統來決定的,無論如何后臺任務都會執行,App的狀態在上述兩種情況下會略有不同。
另外個人觀點:App只是提供了一個注冊后臺任務的平臺,注冊了之后,后臺任務的控制權就交由系統管理了。
9. 后臺任務的執行環境 ?
之前我們已經提到,后臺任務可以在App中運行,也可以在系統提供的主機中運行。如果沒有特別聲明,后臺任務是默認在系統提供的主機中運行的,這樣做有一些好處:
首先,它的啟動同應用程序的狀態無關;
其次,啟動后臺任務更快;
再次,使用的資源更少;
最后,比在App中啟動擁有更高的性能。
如何控制后臺任務在哪個容器下運行呢?這跟Trigger的類型相關:
不同的后臺trigger對于在什么地方運行有不同的限制。對于默認的,沒有指定Executable屬性的時候,后臺任務是運行在系統提供的主機中的。App是不能指定Executable屬性的,如果它必須要在系統提供的主機中與性的。只有那些包含PushNotificationTrigger或者ControlChannelTrigger任務類型的后臺任務才能指定Executable屬性。
10. 后臺任務注冊的持久性?
只要你注冊了一個后臺任務,你就可以永久地擁有它,不論你的App是什么狀態,也不論你的電腦是否關閉過,甚至,不論你的應用程序有沒有更新過。也就是說后臺任務注冊的持久性可以跨越App的更新。
要實現這點必須要注意:你必須要保證你的EntryPoint的一致性,即在新的版本中,同樣的EntryPoint一定要存在。如果不存在了,那么在后臺任務執行中將會出現錯誤。另外,如果我們的新版本中已經不再相應某trigger的后臺任務了,怎么辦?新版本的App可以注冊一個ServicingComplete Trigger的后臺任務,用來提醒當App更新之后,一個未被注冊的后臺任務是失效的。
注意,一旦App被卸載,所有的后臺任務將不復存在。
11. 后臺任務實現中的一些知識點?
后臺任務至少應該設置一個Trigger event。
后臺任務可以沒有condition,也可以有多個conditions。condition擁有門閂行為,意思是,必須所有的condition都滿足,才能啟動后臺任務,而不管Trigger事件有沒有被觸發。這種行為像是鎖住了trigger,等到conditions都滿足了才launch 后臺任務。
后臺任務可以向前臺報告其進度和完成情況:
1 void SampleBackgroundTask::AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration^ task)
2 {
3 auto progress = [this](BackgroundTaskRegistration^ task, BackgroundTaskProgressEventArgs^ args)
4 {
5 auto progress = "Progress: " + args->Progress + "%";
6 BackgroundTaskSample::SampleBackgroundTaskProgress = progress;
7 UpdateUI();
8 };
9 task->Progress += ref new BackgroundTaskProgressEventHandler(progress);
10
11 auto completed = [this](BackgroundTaskRegistration^ task, BackgroundTaskCompletedEventArgs^ args)
12 {
13 UpdateUI();
14 };
15 task->Completed += ref new BackgroundTaskCompletedEventHandler(completed);
16 }
但是需要注意的是,每次啟動的時候都要將這兩個事件關聯到后臺任務上,因為這兩個事件不是持久性的,當App被關閉的時候,他們也會被銷毀。
可以取消后臺任務:
void SampleBackgroundTask::Run(IBackgroundTaskInstance^ taskInstance)
{
taskInstance->Canceled += ref new BackgroundTaskCanceledEventHandler(this, &SampleBackgroundTask::OnCanceled);
//
}
void SampleBackgroundTask::OnCanceled(IBackgroundTaskInstance^ taskInstance, BackgroundTaskCancellationReason reason)
{
CancelRequested = true;
}
注意,取消事件和上述兩個事件是在不同的類型對象中關聯的。
12. 后臺任務的資源管理?
如果App處于前臺,用戶正在與App進行交互的時候,那么后臺任務沒有資源限制。如果一個App沒有在前臺時,限制就出現了。
文章的開頭已經介紹了后臺任務的執行環境是資源受限制的環境,那么哪些資源是受限制的呢?
首先,CPU time,一個Lock Screen App 每15分鐘會獲得2S的CPU時間來處理所有的后臺任務,一個非Lock Screen App每2小時獲得1S的CPU事件來執行后臺任務。這2S是15分鐘的最后2S。如果App使用了所有的CPU事件,那么后臺任務將會等待下一個15分鐘,如果有分配的事件,那么將會執行。

其次,NetWork。如果App處于AC power模式,那么后臺任務沒有網絡資源的限制。Network resource constraints are a function of the amount of energy used by the network interface, which is modeled as the amount of time the network is used to transfer bytes (for download or upload); it is not based on any throughput metrics. The throughput and energy usage of a network interface varies based on factors like link capacity, physical position of the device, or the current load on the WiFi network. WiFi networks typically have lower energy consumption per byte sent and received when compared to cellular mobile networks

LockScreen App在1Mbps帶寬下,每15分鐘可以下載0.469MB的數據,每天下載45MB,在10Mbps帶寬下每15分鐘4.69MB,每天450MB
非LockScreen App 在1Mbps帶寬下,每2小時可以下載0.625MB數據,每天下載7.5MB,在10Mbps帶寬下每2小時6.25MB,每天75MB
13. 后臺任務執行于App中的線程模型?去耦
對于一個非JavaScript App,后臺任務位于in-proc DLL中在MTA中加載。一個真正的后臺任務類可以是STA 或者 MAT線程類型的。因為后臺任務可以在AppSuspended或者Terminated時運行,他們需要同前臺App解耦。在一個分離的單元中加載后臺任務的DLL將強制將后臺任務從App中分離。
當一個App處于Suspended狀態時,UI STA線程被Windows kernel阻塞。這個線程只有當App重新回到Runnning狀態才被release。當app在后臺,并且后臺任務被觸發,App中的所有線程都是非凍結的,除了UI STA線程,并且后臺任務被激活于MTA線程中。UI STA持續被鎖住。如果后臺任務嘗試著訪問位于UI STA 線程中的對象是,將會出現死鎖。為了避免這種情況,后臺任務不能同App共享對象。任何共享的對象都要聚合FTM(Free Threaded Marshaler)。Control Channel trigger描述了這種應用。
在前臺任務和后臺任務之間共享state
Sharing state between the background task and the foreground app
Another aspect to keep in mind if the background task is loaded within the app instead of the default system-provided host executable is that it cannot rely on accessing the memory of the foreground app. Background tasks run regardless of the current state of the app, so background tasks cannot rely on having the app around when they run. The only reliable way for the background task to share state with the app is to use persistent storage, such as ApplicationData, or files.
14. 使用后臺任務的建議?
? Design background tasks to be short lived.
? Design the lock screen user experience as described in the “Guidelines and checklists for lock screen tiles.”
? Do not specify the Executable attribute to ensure the task launches in the system-provided host.
? Describe the background task class name or JavaScript file name accurately in the manifest.
? Look in the event viewer for error messages if the background task is not being activated.
? Use persistent storage to share data between the background task and the app.
? Register for progress and completion handlers in the app.
? Register for a background task cancellation handler in the background task class.
? Register for the ServicingComplete trigger if you expect to update the app.
? Ensure that the background task class library is referenced in the main project and its output type is winmd.
? Describe the triggers in the background manifest accurately.
? Verify if the app needs to be on the lock screen.
? Do not display UI other than toast, tiles or badges from a background task.
? Do not rely on user interaction in background tasks.
15 其他:編程需要注意的細節:?
如果你的后臺任務運行任何的異步程序,那么你需要獲得一個defferral。如果沒有這個deferral,Run方法已經結束,異步方法還沒有結束的情況下后臺任務將會不正常結束。
1 void ExampleBackgroundTask::Run(IBackgroundTaskInstance^ taskInstance)
2 {
3 BackgroundTaskDeferral^ deferral = taskInstance->GetDeferral();
4
5 //
6 // TODO: Modify the following line of code to call a real async function.
7 // Note that the task<void> return type applies only to async
8 // actions. If you need to call an async operation instead, replace
9 // task<void> with the correct return type.
10 //
11 task<void> myTask(ExampleFunctionAsync());
12
13 myTask.then([=] () {
14 deferral->Complete();
15 });
16 }
17
查看后臺任務有沒有注冊,沒有的話注冊一個:
for each (auto task in BackgroundTaskRegistration::AllTasks)//這里保存了本地的所有的注冊了的后臺任務。
{
if(task->Value->Name == "Class1")
{
isRegistered = true;
break;
}
}
if(!isRegistered)
{
RegisterBackgroundTask("TileUpdater", "BackgroundTasks.Class1");
}
這里使用foreach方法比較簡單。
注冊一個后臺任務
1 void MainPage::RegisterBackgroundTask(String^ taskName, String^ entryPoint)
2 {
3 BackgroundTaskBuilder^ btb = ref new BackgroundTaskBuilder();
4 btb->Name = taskName;
5 btb->TaskEntryPoint = entryPoint;
6 btb->SetTrigger(ref new SystemTrigger(SystemTriggerType::InternetAvailable,false));
7
8 BackgroundTaskRegistration^ task = btb->Register();
9 }
我覺得這么總結一下,后臺任務的原理神馬的,大家應該稍微清楚一些了。關于后臺任務,我也看了很久,之前都是糊里糊涂的,今天終于算整明白的,希望對大家有用。歡迎交流。
posted on 2013-01-08 13:00
Dino-Tech 閱讀(3479)
評論(0) 編輯 收藏 引用 所屬分類:
Windows 8