public static void start(string _ConfigFile) { ConfigFile = _ConfigFile; List <jobinfo> list = new List <jobinfo>(); try { if (sched != null) { stop(); sched = null; } sched = new StdSchedulerFactory().GetScheduler(); XmlDocument document = new XmlDocument(); document.Load(ConfigFile); XmlNode node = document.SelectSingleNode("Jobs"); if (node.ChildNodes.Count > 0) { foreach (XmlNode node2 in node.ChildNodes) { jobinfo item = new jobinfo { name = node2.Attributes["name"].Value, type = node2.Attributes["type"].Value, CronExpression = node2.Attributes["CronExpression"].Value, enabled = bool.Parse(node2.Attributes["enabled"].Value) }; if (item.enabled) { list.Add(item); IJobDetail jobDetail = JobBuilder.Create(Type.GetType(item.type)).WithIdentity(item.name, item.name + "Group").Build(); ITrigger trigger = TriggerBuilder.Create().WithIdentity(item.name, item.name + "Group").WithCronSchedule(item.CronExpression).Build(); sched.ScheduleJob(jobDetail, trigger); } } if (list.Count > 0) { sched.Start(); } else { Globals.Debuglog("暂未有计划任务开启1", "_Debuglog.txt"); } } else { Globals.Debuglog("暂未有计划任务开启", "_Debuglog.txt"); } } catch (Exception exception) { Globals.Debuglog(JsonConvert.SerializeObject(list), "_Debuglog.txt"); Globals.Debuglog("启动计划任务失败:" + exception.Message, "_Debuglog.txt"); } }
public static void start(string _ConfigFile) { IUnityContainer container = UnityHelper.GetUnityContainer(); container.RegisterType <MyCodeSqlSugarClient>(new PerThreadLifetimeManager()); //注册缓存对象 container.RegisterType <IMyCodeCacheService, RedisCache>(new InjectionConstructor(SystemConfig.RedisAddress, SystemConfig.CachePrefix)); container.AddNewExtension <QuartzUnityExtension>(); ConfigFile = _ConfigFile; List <jobinfo> list = new List <jobinfo>(); try { if (sched != null) { stop(); sched = null; } //sched = new StdSchedulerFactory().GetScheduler(); sched = container.Resolve <IScheduler>(); XmlDocument document = new XmlDocument(); document.Load(ConfigFile); XmlNode node = document.SelectSingleNode("Jobs"); if (node.ChildNodes.Count > 0) { foreach (XmlNode node2 in node.ChildNodes) { jobinfo item = new jobinfo { name = node2.Attributes["name"].Value, type = node2.Attributes["type"].Value, CronExpression = node2.Attributes["CronExpression"].Value, enabled = bool.Parse(node2.Attributes["enabled"].Value) }; if (item.enabled) { list.Add(item); IJobDetail jobDetail = JobBuilder.Create(Type.GetType(item.type)).WithIdentity(item.name, item.name + "Group").Build(); ITrigger trigger = TriggerBuilder.Create().WithIdentity(item.name, item.name + "Group").WithCronSchedule(item.CronExpression).Build(); sched.ScheduleJob(jobDetail, trigger); } } if (list.Count > 0) { sched.Start(); } else { Console.WriteLine("暂未有计划任务开启1"); } } else { Console.WriteLine("暂未有计划任务开启"); } } catch (Exception exception) { Console.WriteLine(JsonConvert.SerializeObject(list)); Console.WriteLine(exception); LogHelper.Error("出错", exception); } Console.ReadKey(); }
public async Task Start(string _configPath) { ConfigFile = _configPath; var list = new List <jobinfo>(); if (sched != null) { stop(); sched = null; } sched = await _schedulerFactory.GetScheduler(); XmlDocument document = new XmlDocument(); document.Load(ConfigFile); XmlNode node = document.SelectSingleNode("Jobs"); if (node.ChildNodes.Count == 0) { Console.WriteLine("暂未有计划任务开启"); return; } foreach (XmlNode node2 in node.ChildNodes) { jobinfo item = new jobinfo { name = node2.Attributes["name"].Value, type = node2.Attributes["type"].Value, CronExpression = node2.Attributes["CronExpression"].Value, enabled = bool.Parse(node2.Attributes["enabled"].Value), runonce = bool.Parse(node2.Attributes["runonce"].Value) }; if (!item.enabled) { continue; } list.Add(item); IJobDetail jobDetail = JobBuilder.Create(Type.GetType(item.type)).WithIdentity(item.name, item.name + "Group").Build(); ITrigger trigger = null; if (!item.runonce) { trigger = TriggerBuilder.Create().WithIdentity(item.name, item.name + "Group").WithCronSchedule(item.CronExpression).Build(); } else { trigger = TriggerBuilder.Create().WithIdentity(item.name, item.name + "Group").WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(0)).Build(); } await sched.ScheduleJob(jobDetail, trigger); } if (list.Count > 0) { await sched.Start(); } else { Console.WriteLine("暂未有计划任务开启1"); } }