Windows Phone: Don’t call LaunchForTest in Release
Calling ScheduledActionService.LaunchForTest is great for forcing a background task to run. This is important when you are developing and you need to debug your background agent. You may even want to call this in response to user action. You should not do this. The main reason is in Windows Phone 8 calling LaunchForTest, will throw an InvalidOperation exception. You may say that, ‘this works on my machine’, and you’d be right. This is because this exception is thrown only if the application is installed via the Windows Phone store. This does not occur if you sideload the app. Fortunately most of these scenarios are caught during certification testing. However, the Windows Phone certification team will report to you that that app crashes immediately after installation – without much more explanation. You will have a frustrating experience trying to reproduce this bug.
This type of scenario, makes a strong argument for using the beta testing feature of Dev Center that allows you to test the store presentation and installation experience of your app. In fact, by looking at the Dev Center generated crash logs, we found the cause of this crash.
Frame
Image
Function
Offset
0
coredll.dll
xxx_RaiseException
19
1
mscoree3_7.dll
WatsonUnhandledManagedException
296
2
mscoree3_7.dll
Dbg_NotifyManagedException
93
3
mscoree3_7.dll
FirstPassException
1044
4
TransitionStub
0
5
Microsoft.Phone.Scheduler.SystemNotificationInterop.CheckHr
600
6
Microsoft.Phone.Scheduler.SystemNotificationInterop.LaunchNotification
128
7
Microsoft.Phone.Scheduler.ScheduledActionService.LaunchForTest
368
8
BritCo.App.Register_BackGroundAgent_ForLiveTile
240
9
BritCo.App..ctor
124
10
mscoree3_7.dll
IL_CallManaged
884
11
mscoree3_7.dll
BCL_System_Reflection_Invoke
1371
12
mscoree3_7.dll
BCL_System_Reflection_RTMI_Invoke
167
13
mscoree3_7.dll
BCL_System_Reflection_RTCI_Invoke
107
14
System.Reflection.RuntimeConstructorInfo.InternalInvoke
104
15
System.Reflection.RuntimeConstructorInfo.InternalInvoke
1240
16
System.Reflection.ConstructorInfo.Invoke
104
17
.__c__DisplayClass30._GetCreateObjectDelegate_b__2a
92
18
MS.Internal.TypeProxy.CreateInstance
100
19
MS.Internal.FrameworkCallbacks.CreateKnownObject
512
It turns our that the Windows Phone team added this exception to prevent the explicit launching of background agents in store deployed apps. Why? Doing this can cause excessive battery drain and goes against the design of background agents.
Another common reason for wanting to call this API is that you may have code in the background agent you want explicitly triggered by user action. See Shawn Wildermuth’s blog post Confusion Around WP7.1 Periodic Agents for a discussion of this issue and how to code for it.
Thanks,
Mike
-
Jerry