//窗体加载 private void Window_Loaded(object sender, RoutedEventArgs e) { Timer timer = new Timer(); timer.Elapsed += new ElapsedEventHandler(Timer_Click); timer.Interval = 100; timer.Enabled = true; this.lstTask.Dispatcher.Invoke(new Action(delegate { this.lstTask.ItemsSource = _bllTask.GetList(" 1=1 ", null, " CreateDate DESC"); })); OnInit(); }
/// <summary> /// 获取任务列表 /// </summary> /// <param name="isFirstLoad">是否是第一次加载</param> /// <returns></returns> public List <TimedTask.Model.AutoTask> GetTaskList(bool isFirstLoad) { Bll.AutoTask _bllTask = new Bll.AutoTask(); List <TimedTask.Model.AutoTask> list = _bllTask.GetList(" 1=1 ", null, " CreateDate DESC"); //Helper.Instance.GetTaskList(_xml);//任务列表 if (list == null || list.Count == 0) { return(null); } if (isFirstLoad) { List <TimedTask.Model.AutoTask> listTmp = list.Where(m => m.TaskType == "5").ToList <TimedTask.Model.AutoTask>();//锁屏任务 下次启动时间从打开软件算起 if (listTmp.Count > 0) { foreach (TimedTask.Model.AutoTask m in listTmp) { m.NextStartDate = Convert.ToDateTime(Task.Instance.GetNextStartDateByType(Int32.Parse(m.RunType), m.Dayth, null, m.Interval)); _bllTask.Update(m, " Id=" + m.Id); } } listTmp = list.Where(m => m.TaskType == "2").ToList <TimedTask.Model.AutoTask>();//关机任务 日期换成当前年月日 if (listTmp.Count > 0) { foreach (TimedTask.Model.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")) ); _bllTask.Update(m, " Id=" + m.Id); } } } return(list); }
/// <summary> /// 任务启动 /// </summary> public void StartTask() { string proccessName = ""; bool isTask = true;//是否是定时任务 try { List <TimedTask.Model.AutoTask> list = _bllTask.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; } #region 路径不存在 或 不到时间 if (isTask && ( model.ApplicationPath.Length == 0 || model.NextStartDate == null || (model.RunType == RunType.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";//失效 _bllTask.Update(model, " Id=" + model.Id); continue; } #endregion try { #region 失效 if (model.StopDate != null && DateTime.Now >= model.StopDate) { model.Status = "任务过期"; model.Enable = "3"; _bllTask.Update(model, " Id=" + model.Id); continue; } else if (model.Enable != "1") { model.Status = "任务禁用"; model.Enable = "0"; _bllTask.Update(model, " Id=" + model.Id); continue; } #endregion #region 结束进程 if (isTask) { proccessName = model.ApplicationPath.Substring(model.ApplicationPath.LastIndexOf("\\") + 1).Replace(".exe", ""); Helper.Instance.EndApp(proccessName); } #endregion 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.GetNextStartDateByType(Int64.Parse(model.RunType), model.Dayth, null, model.Interval); model.NextStartDate = Convert.ToDateTime(nextSTime); model.Status = result ? "正常" : "启动失败"; model.Enable = (model.RunType == "5") ? "0" : "1";//运行一次 的执行后设置不可用 this._bllTask.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()); } }