定時任務 Wpf.Quartz.Demo.3

先把所有源碼上傳,只是一個Demo,但願你們指點一下不足之處,見本文底部。json

1.設置界面ide

2.詳情頁面this

好了,如今慢慢敘述裏面的一些方法。spa

3.實現拷貝的方法:code

(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);
                }
            }
        }orm

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

(2)用jsonci

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

(3)書本上標準的序列化方法,這裏不作介紹。string

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

暫時永久有效,若是你們以爲很差,我將刪除。

相關文章
相關標籤/搜索