/// <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> async protected override void OnInvoke(ScheduledTask task) { Logger.Log("Agent", "- - - - - - - - - - - - -"); Logger.Log("Agent", "Agent invoked -> " + task.Name); this.contactBindingManager = await ContactBindings.GetAppContactBindingManagerAsync(); // Use the name of the task to differentiate between the ExtensilityTaskAgent // and the ScheduledTaskAgent if (task.Name == "ExtensibilityTaskAgent") { List <Task> inprogressOperations = new List <Task>(); OperationQueue operationQueue = await SocialManager.GetOperationQueueAsync(); ISocialOperation socialOperation = await operationQueue.GetNextOperationAsync(); while (null != socialOperation) { Logger.Log("Agent", "Dequeued an operation of type " + socialOperation.Type.ToString()); try { switch (socialOperation.Type) { case SocialOperationType.DownloadRichConnectData: await ProcessOperationAsync(socialOperation as DownloadRichConnectDataOperation); break; default: // This should never happen HandleUnknownOperation(socialOperation); break; } // The agent can only use up to 20 MB // Logging the memory usage information for debugging purposes Logger.Log("Agent", string.Format("Completed operation {0}, memusage: {1}kb/{2}kb", socialOperation.ToString(), (int)((long)DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage")) / 1024, (int)((long)DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage")) / 1024)); // This can block for up to 1 minute. Don't expect to run instantly every time. socialOperation = await operationQueue.GetNextOperationAsync(); } catch (Exception e) { Helpers.HandleException(e); } } Logger.Log("Agent", "No more operations in the queue"); } NotifyComplete(); }
/// <summary> /// Safely marks operations complete /// </summary> /// <param name="operation">The operation to complete.</param> /// <param name="success">Completion status.</param> internal static void SafeNotifyCompletion(this ISocialOperation operation, bool success = true) { try { operation.NotifyCompletion(success); } catch { // Failures here are not actionable and uninteresting to log // since that info is already present in OS logs } }
public async Task ProcessSocialOperationsQueue() { OperationQueue queue = await SocialManager.GetOperationQueueAsync(); while (true) { ISocialOperation operation = await queue.GetNextOperationAsync(); if (operation != null) { try { switch (operation.Type) { case SocialOperationType.DownloadHomeFeed: /*int num1 = */ await this.ProcessHomeFeed(operation as DownloadHomeFeedOperation); // ? 1 : 0; break; case SocialOperationType.DownloadContactFeed: await this.ProcessContactFeed(operation as DownloadFeedOperation); break; case SocialOperationType.DownloadDashboard: this.ProcessDashboard(operation as DownloadDashboardFeedOperation); break; case SocialOperationType.DownloadRichConnectData: /*int num2 = */ await this.ProcessConnectData(operation as DownloadRichConnectDataOperation); // ? 1 : 0; break; } operation.NotifyCompletion(); } catch { operation.NotifyCompletion(false); } operation = null; } else { break; } } }
private async Task ProcessOperation(ISocialOperation operation) { try { Logger.Log("Agent", "Don't know how to handle the operation " + operation.Type.ToString()); } catch (Exception e) { Helpers.HandleException(e); } finally { operation.SafeNotifyCompletion(); } }
async protected override void OnInvoke(ScheduledTask task) { //TODO: Add code to perform your task in background if (task is PeriodicTask && !task.Name.Equals("ExtensibilityTaskAgent")) { CreateTile(); } else { this.contactBindingManager = await ContactBindings.GetAppContactBindingManagerAsync(); // Use the name of the task to differentiate between the ExtensilityTaskAgent // and the ScheduledTaskAgent if (task.Name == "ExtensibilityTaskAgent") { List <Task> inprogressOperations = new List <Task>(); OperationQueue operationQueue = await SocialManager.GetOperationQueueAsync(); ISocialOperation socialOperation = await operationQueue.GetNextOperationAsync(); while (null != socialOperation) { try { switch (socialOperation.Type) { case SocialOperationType.DownloadRichConnectData: await ProcessOperationAsync(socialOperation as DownloadRichConnectDataOperation); break; } socialOperation = await operationQueue.GetNextOperationAsync(); } catch (Exception e) { } } } } }
/// <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> async protected override void OnInvoke(ScheduledTask task) { Logger.Log("Agent", "- - - - - - - - - - - - -"); Logger.Log("Agent", "Agent invoked -> " + task.Name); this.manager = await OnlineMediaManager.RequestMediaManagerAsync(); // Use the name of the task to differentiate between the ExtensilityTaskAgent // and the ScheduledTaskAgent if (task.Name == "ExtensibilityTaskAgent") { List <Task> inprogressOperations = new List <Task>(); OperationQueue operationQueue = await SocialManager.GetOperationQueueAsync(); ISocialOperation socialOperation = await operationQueue.GetNextOperationAsync(); while (null != socialOperation) { Logger.Log("Agent", "Dequeued an operation of type " + socialOperation.Type.ToString()); try { switch (socialOperation.Type) { case SocialOperationType.DownloadAlbumItems: await ProcessOperation(socialOperation as DownloadAlbumItemsOperation); break; case SocialOperationType.DownloadAlbumCover: await ProcessOperation(socialOperation as DownloadAlbumCoverOperation); break; case SocialOperationType.DownloadImage: // Improve performance by downloading the image binaries in parallel. // The app is limitted to a maximum of 8 simultaneous network requests. // Optimally, the maximum number of parallel operations is between 4-8. // Throttle to 4 parallel image downloads. if (inprogressOperations.Count >= 4) { Task completed = await Task.WhenAny(inprogressOperations); inprogressOperations.Remove(completed); } // Don't wait, download in parallel inprogressOperations.Add(ProcessOperation(socialOperation as DownloadImageOperation)); break; default: // This should never happen await ProcessOperation(socialOperation); break; } // The agent can only use up to 20 MB // Logging the memory usage information for debugging purposes Logger.Log("Agent", string.Format("Completed operation {0}, memusage: {1}kb/{2}kb", socialOperation.ToString(), (int)((long)DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage")) / 1024, (int)((long)DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage")) / 1024)); // This can block for up to 1 minute. Don't expect to run instantly every time. socialOperation = await operationQueue.GetNextOperationAsync(); } catch (Exception e) { Helpers.HandleException(e); } } Logger.Log("Agent", "No more operations in the queue"); // wait for all the operations to complete if (inprogressOperations.Count > 0) { await Task.WhenAll(inprogressOperations); inprogressOperations.Clear(); } } NotifyComplete(); }