Ejemplo n.º 1
0
        /// <summary>
        /// Run the tally for the specified quest.
        /// </summary>
        /// <param name="quest">The quest to scan.</param>
        /// <param name="token">Cancellation token.</param>
        public async Task RunAsync(IQuest quest, CancellationToken token)
        {
            if (quest == null)
            {
                throw new ArgumentNullException(nameof(quest));
            }

            try
            {
                TallyIsRunning = true;
                TallyResults   = string.Empty;

                // Mark the quest as one that we will listen for changes from.
                quest.PropertyChanged -= Quest_PropertyChanged;
                quest.PropertyChanged += Quest_PropertyChanged;

                VoteCounter.ResetUserDefinedTasks(quest.DisplayName);

                var posts = await ForumReader.Instance.ReadQuestAsync(quest, token).ConfigureAwait(false);

                await VoteCounter.TallyPosts(posts, quest, token).ConfigureAwait(false);
            }
            catch (InvalidOperationException e)
            {
                TallyResults += $"\n{e.Message}";
                return;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                //VoteCounter.Instance.Quest = null;
                throw;
            }
            finally
            {
                TallyIsRunning = false;

                // Notify the page provider that we're done, and that the cache
                // can be cleared out as needed:
                ViewModelService.MainViewModel.PageProvider.DoneLoading();

                // Free memory used by loading pages as soon as we're done:
                GC.Collect();
            }

            await UpdateResults(token).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Run the tally for the specified quest.
        /// </summary>
        /// <param name="quest">The quest to scan.</param>
        /// <param name="token">Cancellation token.</param>
        public async Task RunAsync(IQuest quest, CancellationToken token)
        {
            if (quest == null)
            {
                throw new ArgumentNullException(nameof(quest));
            }

            try
            {
                TallyIsRunning = true;
                TallyResults   = string.Empty;

                // Mark the quest as one that we will listen for changes from.
                quest.PropertyChanged -= Quest_PropertyChanged;
                quest.PropertyChanged += Quest_PropertyChanged;

                voteCounter.ResetUserDefinedTasks(quest.DisplayName);

                using (var forumReader = serviceProvider.GetRequiredService <ForumReader>())
                {
                    try
                    {
                        forumReader.StatusChanged += ForumReader_StatusChanged;

                        var(threadTitles, posts) = await forumReader.ReadQuestAsync(quest, token).ConfigureAwait(false);

                        voteCounter.SetThreadTitles(threadTitles);

                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        await TallyPosts(posts, quest, token).ConfigureAwait(false);

                        stopwatch.Stop();
                        logger.LogDebug($"Time to process posts: {stopwatch.ElapsedMilliseconds} ms.");
                    }
                    finally
                    {
                        forumReader.StatusChanged -= ForumReader_StatusChanged;
                    }
                }
            }
            catch (InvalidOperationException e)
            {
                TallyResults += $"\n{e.Message}";
                return;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                //VoteCounter.Instance.Quest = null;
                throw;
            }
            finally
            {
                TallyIsRunning = false;

                // Free memory used by loading pages as soon as we're done:
                GC.Collect();
            }

            await UpdateResults(token).ConfigureAwait(false);
        }