/// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, RoutedEventArgs e) { if (this.mod == null) { MessageBox.Show("没有任何选中项!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } try { this.mod.AudioEnable = this.chkEnable.IsChecked == true ? "1" : "0"; this.mod.AudioVolume = (long)this.sdVolume.Value; this.mod.AudioPath = this.cboAudio.SelectedItem.ToString(); dalAutoTask.Update(this.mod, " Id=" + this.mod.Id); MessageBox.Show("操作成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { Log.SaveLog("NoteListModule btnOK_Click", ex.ToString()); MessageBox.Show("系统异常,操作失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } finally { Bind(); } }
/// <summary> /// 获取任务列表 /// </summary> /// <param name="isFirstLoad">是否是第一次加载</param> /// <returns></returns> public List <Entity.AutoTask> GetTaskList(bool isFirstLoad) { Dal.AutoTask dalAutoTask = new Dal.AutoTask(); List <Entity.AutoTask> list = dalAutoTask.GetList(" 1=1 ", null, " CreateDate DESC"); //Helper.GetTaskList(_xml);//任务列表 if (isFirstLoad) { List <Entity.AutoTask> listTmp = list.Where(m => m.TaskType == "5").ToList <Entity.AutoTask>();//锁屏任务 下次启动时间从打开软件算起 if (listTmp.Count > 0) { foreach (Entity.AutoTask m in listTmp) { m.NextStartDate = Convert.ToDateTime(Task.Instance.GetNextStartDate(Int32.Parse(m.TimeType), m.Dayth, m.Interval)); dalAutoTask.Update(m, " Id=" + m.Id); } } listTmp = list.Where(m => m.TaskType == "2").ToList <Entity.AutoTask>();//关机任务 日期换成当前年月日 if (listTmp.Count > 0) { foreach (Entity.AutoTask m in listTmp) { string nextDate = ((DateTime)m.NextStartDate).ToString("yyyy-MM-dd"); m.NextStartDate = Convert.ToDateTime( ((DateTime)m.NextStartDate).ToString("yyyy-MM-dd HH:mm:00").Replace(nextDate, DateTime.Now.ToString("yyyy-MM-dd")) ); dalAutoTask.Update(m, " Id=" + m.Id); } } } return(list); }
/// <summary> /// 任务启动 /// </summary> public void StartTask() { string proccessName = ""; bool isTask = true;//是否是定时任务 try { List <Entity.AutoTask> list = _dalTask.GetList(" 1=1 ", null, "CreateDate DESC "); if (list == null || list.Count == 0) { return; } foreach (AutoTask model in list) { isTask = true; if (model.TaskType.Length > 0 && model.TaskType != "0")//声音、窗口提醒 { isTask = false; } if (isTask && ( model.ApplicationPath.Length == 0 || model.NextStartDate == null || (model.TimeType == TimeType.Month.ToString() && model.Dayth != DateTime.Now.Day) )) { continue; } if (isTask && !File.Exists(model.ApplicationPath)) { Log.SaveLog("exe_not_exists", "Task StartTask", "任务路径错误,名称:" + model.Title + ",路径:" + model.ApplicationPath + "\r\n"); model.Status = "路径不存在"; model.Enable = "2";//失效 _dalTask.Update(model, " Id=" + model.Id); continue; } try { #region 失效 if (model.StopDate != null && DateTime.Now >= model.StopDate) { model.Status = "任务过期"; model.Enable = "3"; _dalTask.Update(model, " Id=" + model.Id); continue; } else if (model.Enable != "1") { model.Status = "任务禁用"; model.Enable = "0"; _dalTask.Update(model, " Id=" + model.Id); continue; } #endregion if (isTask) { proccessName = model.ApplicationPath.Substring(model.ApplicationPath.LastIndexOf("\\") + 1).Replace(".exe", ""); Helper.EndApp(proccessName); } if (model.NextStartDate != null && DateTime.Now >= model.NextStartDate) { bool result = true; if (isTask) { result = StartApp(model, proccessName); } else { result = StartWarn(model, false); } string nextSTime = Task.Instance.GetNextStartDate(Int64.Parse(model.TimeType), model.Dayth, model.Interval); model.NextStartDate = Convert.ToDateTime(nextSTime); model.Status = result ? "正常" : "启动失败"; model.Enable = (model.TimeType == "5") ? "0" : "1";//运行一次 的执行后设置不可用 _dalTask.Update(model, " Id=" + model.Id); } } catch (Exception ex) { Log.SaveLog("Task StartTask", "更新启动列表配置出错" + ex.ToString() + "\r\n"); } } } catch (Exception e) { Log.SaveLog("Task StartTask", "任务出现异常" + e.ToString()); } }
//保存 private void btnOK_Click(object sender, RoutedEventArgs e) { string path = this.txtPath.Text.Trim(); string title = this.txtTitle.Text.Trim(); string remark = this.txtRemark.Text.Trim(); string startParameter = this.txtStartParameter.Text.Trim(); string startDate = "", stopDate = "", nextStartDate = "";//起始 停止 下次启动时间 if (this.rbtMonth.IsChecked == true) { _timeType = TimeType.Month; } if (this.rbtDay.IsChecked == true) { _timeType = TimeType.Day; } if (this.rbtHour.IsChecked == true) { _timeType = TimeType.Hour; } if (this.rbtMinute.IsChecked == true) { _timeType = TimeType.Minute; } if (this.rbtOnce.IsChecked == true) { _timeType = TimeType.Once; } AutoTask model = new AutoTask(); try { bool flag = ID == 0 ? true : false;//是否是新增 if (this.cboTaskType.SelectedIndex == 0) { if (path.Length == 0 || !path.EndsWith(".exe")) { _statusText = "不是可执行文件或文件路径不能为空!"; return; } } startDate = this.dp_StartDate.Text + " " + ((DateTime)this.dp_StartTime.Value).ToString("HH:mm:00"); stopDate = this.dp_StopDate.Text + " " + Convert.ToDateTime(this.dp_StopTime.Value).ToString("HH:mm:00"); nextStartDate = GetFirstStartDate(); model.StartParameters = startParameter; model.ApplicationPath = path; model.Title = title; model.Enable = (bool)this.cboEnable.IsChecked ? "1" : "0"; model.StartDate = Convert.ToDateTime(startDate); model.StopDate = Convert.ToDateTime(stopDate); model.Remark = remark; model.AudioPath = this._audio; model.TaskType = this.cboTaskType.SelectedIndex.ToString(); model.Status = ""; model.TimeType = ((int)_timeType).ToString(); model.Interval = (this.rbtMinute.IsChecked == true) ? Convert.ToInt32(this.cboMinute.SelectedValue.ToString()) : 0; model.Dayth = (this.rbtMonth.IsChecked == true) ? Convert.ToInt32(this.cboDay.SelectedValue.ToString()) : 0; model.NextStartDate = Convert.ToDateTime(nextStartDate); if (!flag)//修改 { dalAutoTask.Update(model, " Id=" + ID); _statusText = "保存设置成功!"; } else//新增 { TimedTask.Dal.AutoTask dalTask = new Dal.AutoTask(); dalTask.Add(model); _statusText = "新增成功!"; } } catch (Exception ex) { _statusText = "保存设置失败,可能原因是:未找到指定的配置文件!"; Log.SaveLog("TaskDetail btnOK_Click", _statusText + ex.ToString()); } }