Mike Francis: My Mobile Home

Development with Windows Phone, Windows Azure, and Windows 8

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

    I’ve submitted an app 4 times and failed all 4 times with the message that the app terminates silently at launch. I’m hopeful that the issue was caused by the symptom you describe here (e.g. inadvertently including the test launch in the Release version). Great post, thanks!

  • Pingback: Coding Journal | Windows Phone: run background agent every minute

  • SurfRat

    I submitted my app and it passed Microsoft’s scrutiny. I installed it and it immediately threw an exception that luckily I caught. My exception message pointed me to the faulty code and to your site so thanks for sharing this.

    The problem code was:

    #if DEBUG_AGENT

    ScheduledActionService.LaunchForTest(ShakerTorchAgentName, TimeSpan.FromSeconds(60));

    #endif

    Looks like MS stripped out the #if DEBUG_AGENT from the final code so ScheduledActionService.LaunchForTest ran. Wish they had caught the problem in their testing as it had me scratching my head for a while.

  • Jaure

    Thanks, that this conclusion I have arrived, but luckily I have friends with Windows Phone that alerted me that did not load them down the app store in a cell phone that I use for development also gave me problems (that was the problem), I started like to think that was the agent, I discuss my tester friend, and told me that he went out on my application no section of background tasks, and this thought that’s the difference, look at the code again and set me on the line.

    # if DEBUG_AGENT
    ScheduledActionService.LaunchForTest (task.Name, TimeSpan.FromSeconds (60));
    # endif

    And if in spite of the Release Policy DEBUG_AGENT precompiled, it is running?.

    And volia, but do not understand how they could pass the Microsoft test team, I imagine the test mobiles are development, and as they passed me.

    Saludos

  • Pingback: Stop Hatin’ the Scheduled Agents ! ;) | ÇøŋfuzëÐ SøurcëÇødë

Switch to our mobile site