private async void Signin_OnClick(object sender, RoutedEventArgs e) { if (_dashboardViewModel.LoginInfoEnabled) { Login.IsEnabled = false; _dashboardViewModel.LoginInfoEnabled = false; var pageFetcher = ComponentFactory.GetPageFetcher(); var isSignedIn = false; try { var name = _dashboardViewModel.UserName; var password = Password.Password; if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(password)) { //TODO:Use wpf data validation MessageBox.Show("别瞎输、瞎点。"); return; } SigninProgress.Visibility = Visibility.Visible; var authToken = await pageFetcher.Signin(name, password); isSignedIn = true; _configurationManager.SaveAuthConfig(new AuthConfig { UserName = name, AuthToken = authToken }); Password.Password = "******"; _dashboardViewModel.LoginInfoEnabled = false; Login.Content = "退出"; } catch (CannotSigninException cse) { MessageBox.Show(string.IsNullOrEmpty(cse.Message) ? "登录不了,可能是网络不行。" : cse.Message); } finally { Login.IsEnabled = true; SigninProgress.Visibility = Visibility.Hidden; if (!isSignedIn) { _dashboardViewModel.LoginInfoEnabled = true; if (string.IsNullOrWhiteSpace(_dashboardViewModel.UserName)) { UserNameBox.Focus(); } else { Password.Focus(); } } } } else { ComponentFactory.GetPageFetcher().Signout(); _configurationManager.SaveAuthConfig(new AuthConfig { UserName = _dashboardViewModel.UserName, AuthToken = "" }); _dashboardViewModel.LoginInfoEnabled = true; Password.Clear(); Password.Focus(); Login.Content = "登录"; } }
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; }