Esempio n. 1
0
        /// <summary>
        /// Performs the work of a background task. The system calls this method when the associated background task has been triggered.
        /// </summary>
        /// <param name="taskInstance">An interface to an instance of the background task. The system creates this instance when the task has been triggered to run.</param>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            taskInstance.Canceled += taskInstance_Canceled;
            BackgroundTaskDeferral deferral = null;

            try
            {
                // Get the background task deferral
                deferral = taskInstance.GetDeferral();
                LogFile.Instance.LogInformation("", "", string.Format("Run: IsUISuspended - {0}, IsTaskRunning - {1}.", StorageSettings.IsUISuspended, StorageSettings.IsTaskRunning));

                // If the ui is suspended
                if (StorageSettings.IsUISuspended)
                {
                    // Mark background task as completed
                    StorageSettings.IsTaskRunning = true;
                    LogFile.Instance.LogInformation(this.GetType().Name, "", "Background Task Started.");

                    // Create the cancellation token source
                    _cancellationTokenSource = new CancellationTokenSource();

                    // Create the mail clients list
                    _mailClients = new List <MailClient>();

                    // Loop through each setting
                    await StorageSettings.InitialiseAsync();

                    Parallel.ForEach(StorageSettings.AccountSettingsDataDictionary.Values, async(accountSettingsData) =>
                    {
                        // Create the mail client
                        MailClient mailClient         = await MailClient.GetMailClient(accountSettingsData);
                        mailClient.UpdatedMessage    += mailClient_UpdatedMessage;
                        mailClient.DeletedMessage    += mailClient_DeletedMessage;
                        mailClient.DownloadedMessage += mailClient_DownloadedMessage;

                        // Login
                        if (mailClient.Login().IsSuccessfull)
                        {
                            LogFile.Instance.LogInformation(mailClient.AccountSettingsData.EmailAddress, this.GetType().Name, "Logged in.");

                            // Download unread messages
                            LogFile.Instance.LogInformation(mailClient.AccountSettingsData.EmailAddress, this.GetType().Name, "Download started...");
                            _mailClients.Add(mailClient);
                            await mailClient.DownloadUnreadMessagesTask(_cancellationTokenSource.Token);
                            LogFile.Instance.LogInformation(mailClient.AccountSettingsData.EmailAddress, this.GetType().Name, "Download completed.");

                            // Logout
                            await mailClient.Logout();
                            LogFile.Instance.LogInformation(mailClient.AccountSettingsData.EmailAddress, this.GetType().Name, "Logged out.");

                            // Unsubscribe from events
                            mailClient.DownloadedMessage -= mailClient_DownloadedMessage;
                            mailClient.UpdatedMessage    -= mailClient_UpdatedMessage;
                            mailClient.DeletedMessage    -= mailClient_DeletedMessage;

                            // Remove mail client
                            _mailClients.Remove(mailClient);

                            // Save
                            await StorageSettings.SaveMailHeaderDictionary();
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                LogFile.Instance.LogError(this.GetType().Name, "", ex.ToString());
            }
            finally
            {
                // Remove the cancellation token
                _cancellationTokenSource = null;

                // Mark background task as completed
                StorageSettings.IsTaskRunning = false;
                LogFile.Instance.LogInformation(this.GetType().Name, "", "Background Task Completed.");
                deferral.Complete();
            }
        }