/// <summary>
        /// Agent that runs a scheduled task
        /// </summary>
        /// <param name="task">
        /// The invoked task
        /// </param>
        /// <remarks>
        /// This method is called when a periodic or resource intensive task is invoked
        /// </remarks>
        protected override void OnInvoke(ScheduledTask task)
        {
#if DEBUG
            AgentExitReason reason = task.LastExitReason;
            DebugUtility.SaveDiagnostics(new Diagnostics()
            {
                DeviceUniqueId = DebugUtility.GetDeviceUniqueID(), AgentExitReason = reason.ToString()
            });
            Debug.WriteLine("Agent Last Exited for Reason: " + reason.ToString());
            DebugUtility.DebugStartStopwatch();
            DebugUtility.DebugOutputMemoryUsage("Scheduled Task Initial Memory Snapshot");
#endif
            source = new PhoneBirthdaySource();
            source.GetEntriesCompleted += source_GetEntriesCompleted;
            source.BeginGetAllEntries();
        }
Esempio n. 2
0
        public void StartAgent(bool debugAgent = false)
        {
            // Obtain a reference to the period task, if one exists
            var periodicTask = ScheduledActionService.Find(_taskName) as PeriodicTask;

            // If the task already exists and background agents are enabled for the
            // application, you must remove the task and then add it again to update
            // the schedule
            if (periodicTask != null)
            {
                AgentExitReason reason = periodicTask.LastExitReason;
                //ISettingsService settings = KernelService.Kernel.Get<ISettingsService>();
                //settings.SaveSetting<AgentExitReason>(reason, "AgentLastExitReason");
                //				_logger.Debug("Agent Last Exited for Reason: " + reason.ToString());
                DebugUtility.SaveDiagnostics(new Diagnostics()
                {
                    DeviceUniqueId = DebugUtility.GetDeviceUniqueID(), AgentExitReason = reason.ToString()
                });
                RemoveAgent();
            }

            periodicTask = new PeriodicTask(_taskName);

            // The description is required for periodic agents. This is the string that the user
            // will see in the background services Settings page on the device.
            periodicTask.Description    = Resources.AgentDescription;
            periodicTask.ExpirationTime = DateTime.Now.AddDays(14);

            // Place the call to Add in a try block in case the user has disabled agents.
            try
            {
                ScheduledActionService.Add(periodicTask);
                // If debugging is enabled, use LaunchForTest to launch the agent in one minute.
#if (DEBUG_AGENT)
                ScheduledActionService.LaunchForTest(_taskName, TimeSpan.FromSeconds(60));
#endif
#if DEBUG
                if (debugAgent)
                {
                    ScheduledActionService.LaunchForTest(_taskName, TimeSpan.FromSeconds(60));
                }
#endif
            }
            catch (InvalidOperationException exception)
            {
                if (exception.Message.Contains("BNS Error: The action is disabled"))
                {
                    throw new AgentManagementException(Resources.AgentsDisabledByUserError);
                }
                if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
                {
                    if (!AreAgentsSupported)
                    {
                        throw new AgentManagementException(Resources.AgentsNotSupportedError);
                    }
                    else
                    {
                        throw new AgentManagementException(Resources.TooManyAgentsError);
                    }
                }
            }
        }