private async Task CheckForScheduledTasks() { var taskKey = TaskSchedulerUtils.ComputeKey(Config.Default.DataDir); TaskUtils.RunOnUIThreadAsync(() => { try { var backupState = TaskSchedulerUtils.TaskStateAutoBackup(taskKey, null); var updateState = TaskSchedulerUtils.TaskStateAutoUpdate(taskKey, null); this.AutoBackupState = backupState.ToString(); this.AutoUpdateState = updateState.ToString(); Logger.Debug("CheckForScheduledTasks performed"); } catch (Exception) { // Ignore. } }).DoNotWait(); await Task.Delay(Config.Default.ScheduledTasksCheckTime * 1 * 1000); this.scheduledTaskChecker.PostAction(CheckForScheduledTasks).DoNotWait(); }
private async Task CheckForScheduledTasks() { var taskKey = TaskSchedulerUtils.ComputeKey(Config.Default.DataDir); TaskUtils.RunOnUIThreadAsync(() => { try { var backupState = TaskSchedulerUtils.TaskStateAutoBackup(taskKey, null, out DateTime backupnextRunTime); var updateState = TaskSchedulerUtils.TaskStateAutoUpdate(taskKey, null, out DateTime updatenextRunTime); this.AutoBackupState = backupState; this.AutoUpdateState = updateState; this.AutoBackupStateString = GetTaskStateString(AutoBackupState); this.AutoUpdateStateString = GetTaskStateString(AutoUpdateState); this.AutoBackupNextRunTime = backupnextRunTime == DateTime.MinValue ? string.Empty : $"{_globalizer.GetResourceString("MainWindow_TaskRunTimeLabel")} {backupnextRunTime.ToString("G")}"; this.AutoUpdateNextRunTime = updatenextRunTime == DateTime.MinValue ? string.Empty : $"{_globalizer.GetResourceString("MainWindow_TaskRunTimeLabel")} {updatenextRunTime.ToString("G")}"; Logger.Debug("CheckForScheduledTasks performed"); } catch (Exception) { // Ignore. } }).DoNotWait(); await Task.Delay(Config.Default.ScheduledTasksCheckTime * 1 * 1000); this.scheduledTaskChecker.PostAction(CheckForScheduledTasks).DoNotWait(); }
/// <summary> /// 启动定时任务计划 /// </summary> #region public void startTask() public void startTask() { //获取定时任务的配置参数 //任务名称 String name = CommonUtils.getUpdateName(); //任务描述信息 String describe = CommonUtils.getUpdateDescribe(); //定时任务更新频率 String frequency = CommonUtils.getFrequency(); //定时任务开始执行的日期 String date = CommonUtils.getDate(); //定时任务执行的天数,在frequency设置为monthly有效 int day = CommonUtils.getDay(); //设置定时任务在星期几执行,在frequency设置为weekly有效 String week = CommonUtils.getWeek(); //定时任务执行的命令的文件所在路径 String path = String.Format(@"{0}HotelUpdateService.exe", CommonUtils.getServiceRunningPath()); //记录定时任务的状态 _TASK_STATE state; //判断定时任务是否存在不存在则创建定时任务 if (!TaskSchedulerUtils.checkTask(name, out state)) { //无限循环,直到定时任务创建成功 for (; ;) { //创建定时任务 bool flag = TaskSchedulerUtils.createTask(Environment.UserName, describe, name, path, frequency, date, day, week); if (flag) { Logger.info(typeof(UpdateController), String.Format("create task {0} success.", name)); //定时任务创建成功以后,杀死当前进程 Process.GetCurrentProcess().Kill(); return; } Logger.warn(typeof(UpdateController), String.Format("create task {0} failed, try again.", name)); } } //根据定时任务状态,判断是否需要启动定时任务 if (state != _TASK_STATE.TASK_STATE_RUNNING && state != _TASK_STATE.TASK_STATE_READY) { Logger.warn(typeof(UpdateController), String.Format("task {0} 's state is {1}, waiting for start.", name, state.ToString())); TaskSchedulerUtils.startTask(name); Process.GetCurrentProcess().Kill(); } else if (state == _TASK_STATE.TASK_STATE_READY) { Logger.info(typeof(UpdateController), String.Format("task {0} 's state is {1}", name, state)); Process.GetCurrentProcess().Kill(); } }
private void AutoUpdateTaskRun_Click(object sender, RoutedEventArgs e) { var taskKey = TaskSchedulerUtils.ComputeKey(Config.Default.DataDir); try { TaskSchedulerUtils.RunAutoUpdate(taskKey, null); } catch (Exception) { // Ignore. } }
protected override void OnClosed(EventArgs e) { if (SecurityUtils.IsAdministrator()) { // check if the Auto Update has been enabled. if (Config.Default.AutoUpdate_EnableUpdate) { // check if an update period has been set. if (Config.Default.AutoUpdate_UpdatePeriod <= 0) { MessageBox.Show(_globalizer.GetResourceString("GlobalSettings_CacheUpdate_DisabledLabel"), _globalizer.GetResourceString("GlobalSettings_CacheUpdate_DisabledTitle"), MessageBoxButton.OK, MessageBoxImage.Information); return; } // check if the cache directory has been set and it exists. if (string.IsNullOrWhiteSpace(Config.Default.AutoUpdate_CacheDir) || !Directory.Exists(Config.Default.AutoUpdate_CacheDir)) { MessageBox.Show(_globalizer.GetResourceString("GlobalSettings_CacheDirectory_ErrorLabel"), _globalizer.GetResourceString("GlobalSettings_CacheDirectory_ErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error); return; } } var taskKey = TaskSchedulerUtils.ComputeKey(Config.Default.DataDir); var command = Assembly.GetEntryAssembly().Location; if (!TaskSchedulerUtils.ScheduleAutoUpdate(taskKey, null, command, Config.Default.AutoUpdate_EnableUpdate ? Config.Default.AutoUpdate_UpdatePeriod : 0)) { MessageBox.Show(_globalizer.GetResourceString("GlobalSettings_CacheTaskUpdate_ErrorLabel"), _globalizer.GetResourceString("GlobalSettings_CacheTaskUpdate_ErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error); } else { if (Config.Default.AutoUpdate_EnableUpdate && Config.Default.AutoUpdate_UpdatePeriod > 0) { MessageBox.Show(String.Format(_globalizer.GetResourceString("GlobalSettings_CacheUpdate_EnabledLabel"), Config.Default.AutoUpdate_UpdatePeriod), _globalizer.GetResourceString("GlobalSettings_CacheUpdate_EnabledTitle"), MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show(_globalizer.GetResourceString("GlobalSettings_CacheUpdate_DisabledLabel"), _globalizer.GetResourceString("GlobalSettings_CacheUpdate_DisabledTitle"), MessageBoxButton.OK, MessageBoxImage.Information); } } } if (Config.Default.SteamCmdRedirectOutput && !Config.Default.SteamCmd_UseAnonymousCredentials) { MessageBox.Show(_globalizer.GetResourceString("GlobalSettings_SteamCMDAuthentication_DisabledLabel"), _globalizer.GetResourceString("GlobalSettings_SteamCMDAuthentication_DisabledTitle"), MessageBoxButton.OK, MessageBoxImage.Error); Config.Default.SteamCmd_UseAnonymousCredentials = true; } Config.Default.Save(); base.OnClosed(e); }
private void AutoUpdateTaskState_Click(object sender, RoutedEventArgs e) { if (!IsAdministrator) { MessageBox.Show(_globalizer.GetResourceString("MainWindow_TaskAdminErrorLabel"), _globalizer.GetResourceString("MainWindow_TaskAdminErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error); return; } var taskKey = TaskSchedulerUtils.ComputeKey(Config.Default.DataDir); try { TaskSchedulerUtils.SetAutoUpdateState(taskKey, null, null); } catch (Exception) { // Ignore. } }
/// <summary> /// 启动定时任务计划 /// </summary> #region public void startTask() public void startTask() { //启动定时任务计划 String name = CommonUtils.getUpdateName(); String describe = CommonUtils.getUpdateDescribe(); String frequency = CommonUtils.getFrequency(); String date = CommonUtils.getDate(); int day = CommonUtils.getDay(); String week = CommonUtils.getWeek(); String path = String.Format(@"{0}HotelUpdateService.exe", CommonUtils.getServiceRunningPath()); _TASK_STATE state; if (!TaskSchedulerUtils.checkTask(name, out state)) { for (; ;) { bool flag = TaskSchedulerUtils.createTask(Environment.UserName, describe, name, path, frequency, date, day, week); if (flag) { Process.GetCurrentProcess().Kill(); return; } } } if (state != _TASK_STATE.TASK_STATE_RUNNING && state != _TASK_STATE.TASK_STATE_READY) { Logger.info(typeof(UpdateController), String.Format("task {0} 's state is {1}, waiting for start.", name, state.ToString())); TaskSchedulerUtils.startTask(name); Process.GetCurrentProcess().Kill(); } else if (state == _TASK_STATE.TASK_STATE_READY) { Logger.info(typeof(UpdateController), String.Format("task {0} 's state is {1}", name, state)); Process.GetCurrentProcess().Kill(); } }