Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScheduledActionService.Find throws ArgumentException

I have a Windows Phone 7 app deployed in the marketplace that updates its Live Tile via a PeriodicTask Background Agent.

One user is reporting issues with the tile no longer updating after it has been working for some time.

When they check the background tasks on the phone it is disabled and the checkbox to "Turn background tasks back on for this application the next time I open it" is checked.

After opening the app and trying the pin operation again the background task hasn't resumed.

I suspect this may be related to two crash reports I've seen in the App Hub:

Problem Function: Microsoft.Phone.Scheduler.SystemNotificationInterop.CheckHr

Exception Type: ArgumentException

Stack Trace:

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 248
6 Microsoft.Phone.Scheduler.SystemNotificationInterop.GetNotificationByID 156
7 Microsoft.Phone.Scheduler.ScheduledActionService.Find 276
8 MyApp.Agents.TaskIsActive 60
9 MyApp.MainPage.SetupApplicationBar 44
10 MyApp.MainPage.MainPage_Loaded 100
11 MS.Internal.CoreInvokeHandler.InvokeEventHandler 3660
12 MS.Internal.JoltHelper.FireEvent 1348
13 mscoree3_7.dll IL_CallManaged 884
14 mscoree3_7.dll IL_CallDelegateInternal 176
15 mscoree3_7.dll makeComPlusCall 5255
16 mscoree3_7.dll makeComPlusCallReturnInt 21
17 0
18 agcore.dll CCoreServices::CLR_FireEvent 385

Calls to Microsoft.Phone.Scheduler.ScheduledActionService.Find are resulting in an ArgumentException.

The name parameter I am calling the Find method with is coming from a private const string so the value will be the same with each call.

Should I just catch this exception and assume the background agent isn't present or is it indicating something is wrong with the agent?

At this stage I can't reproduce the exception when running the app in the emulator.


"When [the] Background Agent crashes two times in sequence, it's removed from scheduling"

I've tried deliberately crashing the ScheduledAgent on every invocation as follows:

protected override void OnInvoke(ScheduledTask task)
{
    UpdateTile();

#if DEBUG
    // If we're debugging, fire the task again
    ScheduledActionService.LaunchForTest("MyScheduledTaskAgent", new TimeSpan(0, 0, 30));
    throw new Exception("Bang");
#endif

     NotifyComplete();
}

This does cause the background task to turn off under the settings in the emulator after two invocations. However, if I reopen the app calls to ScheduledActionService.Find work without an exception. I can also remove the failed PeriodicTask and add a new instance without issue.


"an exception can be thrown when the background agent is deactivated in the phone's settings. I think in that case the exception is thrown on ScheduledActionService.Add, not ScheduledActionService.Find"

I tried this in the emulator. I get the following exception from ScheduledActionService.Add(task);:

System.InvalidOperationException - "BNS Error: The action is disabled\r\n"

Calls to ScheduledActionService.Find still work fine.

like image 625
Daniel Ballinger Avatar asked Nov 13 '22 05:11

Daniel Ballinger


1 Answers

I managed to reproduce the ArgumentException and StackTrace in the emulator and on a handset attached to my PC.

The steps were:

  1. Set a break point in an exception handler around the call to ScheduledActionService.Find(TASK_NAME)
  2. Start the app up in the emulator (or handset) with the debugger attached
  3. Use the pin menu item to start the PeriodicTask background agent. Note that in debug mode I call ScheduledActionService.LaunchForTest(TASK_NAME, new TimeSpan(0, 0, 1)); immediately after adding the PeriodicTask.
  4. Navigate to a sub page in my app.
  5. Quickly use the back button to return to the MainPage and then again to quit the applicaiton. The Loaded event on the MainPage calls SetupApplicationBar(), which ultimately calls the ScheduledActionService.Find() method.
  6. As the application is shutting down the exception will occur.

Exception Type: ArgumentException

Message: E_INVALIDARG

StackTrace:

at Microsoft.Phone.Scheduler.SystemNotificationInterop.CheckHr(Int32 hr)
at Microsoft.Phone.Scheduler.SystemNotificationInterop.GetNotificationByID(Guid notificationID)
at Microsoft.Phone.Scheduler.ScheduledActionService.Find(String name)
at SolarCalculator.Agents.TaskIsActive()
at SolarCalculator.MainPage.SetupApplicationBar()
at SolarCalculator.MainPage.MainPage_Loaded(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Given that the app is shutting down and I'm only trying to figure out if the background agent is running I figure it is safe to catch the exception and return false from my TaskIsActive() method.

Now that I know the message from the ArgumentException is E_INVALIDARG I found Setting alarm in Windows Phone 7 which describes getting the same error when making calls to ScheduleActionService in the Application_Exit event.

like image 150
Daniel Ballinger Avatar answered Dec 24 '22 12:12

Daniel Ballinger