Пример #1
0
 public TaskQueueManager(IPageFetcher pageFetcher, IPageProcessor pageProcessor, Action <string> outputAction)
 {
     _pageFetcher           = pageFetcher;
     _pageProcessor         = pageProcessor;
     _outputAction          = outputAction;
     _pageSaveJobRunner     = new PostSaveJobRunner(ComponentFactory.GetPostRepository());
     _runningInfoRepository = ComponentFactory.GetRunningInfoRepository();
 }
Пример #2
0
        private async void Run_OnClick(object sender, RoutedEventArgs e)
        {
            if (_cts != null)
            {
                _cts.Cancel();
                var newCts = new CancellationTokenSource();
                _cts = newCts;
                return;
            }
            if (!ComponentFactory.GetPageFetcher().HasAuthToken)
            {
                MessageBox.Show("先登录。");
                Run.Content = "运行";
                return;
            }
            Run.Content            = "取消";
            RunProgress.Visibility = Visibility.Visible;
            Login.IsEnabled        = false;
            OutputBox.Clear();
            var outputCount = 0;

            _taskManager = new TaskQueueManager(ComponentFactory.GetPageFetcher(),
                                                ComponentFactory.GetPageProcessor(), s =>
            {
                Action output = () =>
                {
                    if (outputCount++ > 200)
                    {
                        OutputBox.Clear();
                        outputCount = 0;
                    }
                    OutputBox.AppendText(s + Environment.NewLine);
                    OutputBox.ScrollToEnd();
                };
                if (Thread.CurrentThread == OutputBox.Dispatcher.Thread)
                {
                    output();
                }
                else
                {
                    OutputBox.Dispatcher.InvokeAsync(output);
                }
            });

            _cts             = new CancellationTokenSource();
            _app.RunningInfo = await ComponentFactory.GetRunningInfoRepository().GetLastUncompletedAsync();

            if (_app.RunningInfo != null)
            {
                var runLastUncompletedTask = MessageBox.Show(string.Format("上次开始于{0}的任务没运行完成,要继续吗?\r\n选择\"是\"继续运行,选择\"否\"开始运行新任务。", _app.RunningInfo.StartTime),
                                                             "", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
                if (!runLastUncompletedTask)
                {
                    _app.RunningInfo.IsCompleted   = true;
                    _app.RunningInfo.LastSavedTime = DateTime.Now;
                    await ComponentFactory.GetRunningInfoRepository().SaveAsync(_app.RunningInfo);

                    _app.RunningInfo = null;
                }
            }
            if (_app.RunningInfo == null)
            {
                var date = GetExpirationDate();
                _app.RunningInfo = new RunningInfo
                {
                    InitialEntryPointUrl  = GetEntryPointUrl(),
                    InitialExpirationDate = date,
                    CurrentExpirationDate = date,
                    Mode      = GetRunningMode(),
                    StartTime = DateTime.Now,
                };
            }

            try
            {
                await _taskManager.Run(_app.RunningInfo, _cts.Token);
            }
            catch (OperationCanceledException)
            {
                MessageBox.Show("已取消。");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                MessageBox.Show(ex.Message);
            }
            finally
            {
                _app.RunningInfo = null;
            }
            Run.Content            = "运行";
            RunProgress.Visibility = Visibility.Hidden;
            Login.IsEnabled        = true;
            _cts = null;
        }