ScheduledAgent.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Reactive.Linq;
  3. using System.Windows;
  4. using Microsoft.Phone.Scheduler;
  5. using Microsoft.Phone.Shell;
  6. namespace WindowsPhoneAgent7
  7. {
  8. public class ScheduledAgent : ScheduledTaskAgent
  9. {
  10. private static volatile bool _classInitialized;
  11. /// <remarks>
  12. /// ScheduledAgent constructor, initializes the UnhandledException handler
  13. /// </remarks>
  14. public ScheduledAgent()
  15. {
  16. if (!_classInitialized)
  17. {
  18. _classInitialized = true;
  19. // Subscribe to the managed exception handler
  20. Deployment.Current.Dispatcher.BeginInvoke(delegate
  21. {
  22. Application.Current.UnhandledException += ScheduledAgent_UnhandledException;
  23. });
  24. }
  25. }
  26. /// Code to execute on Unhandled Exceptions
  27. private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  28. {
  29. if (System.Diagnostics.Debugger.IsAttached)
  30. {
  31. // An unhandled exception has occurred; break into the debugger
  32. System.Diagnostics.Debugger.Break();
  33. }
  34. }
  35. /// <summary>
  36. /// Agent that runs a scheduled task
  37. /// </summary>
  38. /// <param name="task">
  39. /// The invoked task
  40. /// </param>
  41. /// <remarks>
  42. /// This method is called when a periodic or resource intensive task is invoked
  43. /// </remarks>
  44. protected override void OnInvoke(ScheduledTask task)
  45. {
  46. //TODO: Add code to perform your task in background
  47. //
  48. // Goal of the agent project is simply to test the "Marketplace Test Kit" passes.
  49. // If Rx uses the following, it won't pass.
  50. //
  51. //PhoneApplicationService.Current.Activated += (o, e) => { };
  52. Observable.Return("").Delay(TimeSpan.FromSeconds(1)).Subscribe(_ =>
  53. {
  54. NotifyComplete();
  55. });
  56. }
  57. }
  58. }