async private void Login_Click(object sender, RoutedEventArgs e)
        {
            // Initialize online media manager
            OnlineMediaManager onlineMediaManager = await OnlineMediaManager.RequestMediaManagerAsync();

            await onlineMediaManager.ProvisionAsync();

            MessageBox.Show("Done!");
        }
        async private void Logout_Click(object sender, RoutedEventArgs e)
        {
            // Uninitialize online media manager
            OnlineMediaManager onlineMediaManager = await OnlineMediaManager.RequestMediaManagerAsync();

            // Must wait for it to complete before logging back in
            await onlineMediaManager.DeprovisionAsync();

            MessageBox.Show("Done!");
        }
        /// <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();
        }
        /// <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();
        }