定时任务 Wpf.Quartz.Demo.3

先把全部源码上传,只是一个Demo,希望大家指点一下不足之处,见本文底部。

1.设置界面

2.详情页面

好了,现在慢慢叙述里面的一些方法。

3.实现拷贝的方法:

(1) public static void LeftCopyRight(object left,  object right)
        {
            var Properties = left.GetType().GetProperties();
            foreach (var Propertie in Properties)
            {
                //循环遍历属性
                if (Propertie.CanRead && Propertie.CanWrite)
                {
                    //进行属性拷贝
                    Propertie.SetValue(left, Propertie.GetValue(right, null), null);
                }
            }
        }

 CopyHelper.LeftCopyRight(run, (this.DataContext as SetWindowViewModel).Run);

(2)用json

 this.Run = JsonConvert.DeserializeObject<BaseRunner>(JsonConvert.SerializeObject(run));

(3)书本上标准的序列化方法,这里不做介绍。

4.读取保存在本地json格式的配置文件

 try
            {
                if (!File.Exists(JsonPath))  // 判断是否已有相同文件
                {
                    FileStream fs = new FileStream(JsonPath, FileMode.Create, FileAccess.ReadWrite);
                    fs.Close();
                }

                localRuns = JsonConvert.DeserializeObject<List<BaseRunner>>(File.ReadAllText(JsonPath));
            }
            catch (Exception ex)
            {
                log.Fatal(ex);
            }

保存配置文件

 try
            {
                File.WriteAllText(JsonPath, JsonConvert.SerializeObject(TaskRuns.Select(p => p as BaseRunner)));
            }
            catch (Exception ex)
            {
                log.Fatal(ex);
            }

文件位置 public static string JsonPath = System.AppDomain.CurrentDomain.BaseDirectory + "JobTasks.json";

5.里面一共添加了3个任务,使用反射的方法,需要添加新的任务,只需要按照Jobs下HelloJob建立任务即可。

然后在系统启动的时候把你的任务添加上。这里特别希望有个朋友指点一下,如何能够不用手动加的方法,如何将反射直接用在泛型方法上,这样启动就可自动启动了。

TaskRuns = new List<IRun>();
            try
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                Type[] types = asm.GetTypes();

                foreach (Type t in types)
                {
                    if (new ArrayList(t.GetInterfaces()).Contains(typeof(IJob)))
                    {
                        IJob job = ObjectUtils.InstantiateType<IJob>(t);
                        if (job != null)
                        {
                            IRun run = null;
                            if (job is HelloJob)
                            {
                                run = new SimpleRunner<HelloJob>();
                            }
                            else if (job is HelloJob2)
                            {
                                run = new SimpleRunner<HelloJob2>();
                            }
                            else if (job is HelloJob3)
                            {
                                run = new SimpleRunner<HelloJob3>();
                            }

                            if (run != null)
                            {
                                if (localRuns != null)
                                {
                                    var localRun = localRuns.Where(p => p.Name == run.Name).FirstOrDefault();
                                    if (localRun != null)
                                    {
                                        CopyHelper.LeftCopyRight(run, localRun);                                        
                                    }
                                }
                                if (run.TriggerState != TriggerState.Normal || run.Mode == Mode.Hand)
                                {
                                    run.TriggerState = TriggerState.None;
                                }
                                run.CronSecondSet.Init();
                                run.CronMinuteSet.Init();
                                run.CronHourSet.Init();
                                run.CronDaySet.Init();
                                run.CronMonthSet.Init();
                                run.CronWeekSet.Init();
                                run.CronYearSet.Init();
                                run.LogOut = this.LogOut;
                                run.IsEdit = false;
                                TaskRuns.Add(run);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Fatal(ex);
            }

View Code

 6.Cron与DateTime互转

public class CronHelper
    {
        public static string DateTime2Cron(DateTime date)
        {
            return date.ToString("ss mm HH dd MM ? yyyy");
        }

        public static DateTime Cron2DateTime(string cron)
        {
            return DateTime.ParseExact(cron, "ss mm HH dd MM ? yyyy", System.Globalization.CultureInfo.CurrentCulture);
        }

        public static DateTimeOffset DateTime2DateTimeOffset(DateTime datetime)
        {
            return DateTime.SpecifyKind(datetime, DateTimeKind.Unspecified);
        }

        public static DateTime DateTimeOffset2DateTime(DateTimeOffset datetimeoffset)
        {
            return datetimeoffset.DateTime;
        }
    }
CronHelper

最后,废话少说,上

链接:https://pan.baidu.com/s/1DpY8Tzwd1ggXVQkPH4bSlw
提取码:eon2

暂时永久有效,如果大家觉得不好,我将不再分享。

猜你喜欢

转载自www.cnblogs.com/akwkevin/p/10423905.html