开发的时候经常有固定某时操作某资源的需求,解决这种需求最简单的就是定时任务。
如果没有特别刁钻的要求,建议直接找来用,比如这一款:TimedJob
![](https://upload-images.jianshu.io/upload_images/19112139-fbc7342de248834d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下载完以后去Startup里设置一下:
![](https://upload-images.jianshu.io/upload_images/19112139-313e2adcc368523d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19112139-9adb6a55a8b8c504.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
再然后新建一个类,继承Job,添加特性,如下:
![](https://upload-images.jianshu.io/upload_images/19112139-09727b183d2e135b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
解释一下:
Begin:任务开始时间
Interval:间隔(毫秒)
SkipWhileExecuting:是否等待上一个任务结束,true等待
定好参数就能跑起来了。
这里我有一个小建议,如果要开多个定时任务,并业务逻辑相对复杂的话,尽量不要这样写:
![](https://upload-images.jianshu.io/upload_images/19112139-d0b3bacf4fc85b06.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
建议这样写:
![](https://upload-images.jianshu.io/upload_images/19112139-c770cf4c86a9e3e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
~~写点水文,存点资料