定时任务 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; } }
最后,废话少说,上
链接:https://pan.baidu.com/s/1dpy8tzwd1ggxvqkph4bslw
提取码:eon2
暂时永久有效,如果大家觉得不好,我将删除。