作业调度小软件
最近,由于工作需要,做了一个作业调度的小软件。先上两张效果图。
一、效果图
作业启动状态
作业停止状态
二、Quartz
调度的核心库用的是 Quartz.NET。官方网站:http://quartznet.sourceforge.net/。这里有更多的中文介绍。
先创建一个类库,新建一个TaskJob类用来执行任务。
using System.Collections.Generic;
using System.Text;
using Quartz;
namespace MyJob
{
public class TaskJob : IJob
{
public void Execute(JobExecutionContext context)
{
//todo:此处为执行的任务
string path = AppDomain.CurrentDomain.BaseDirectory + "log\\Log.txt";
Common.writeInLog("开始执行任务。", path);
//。。。
Common.writeInLog("执行已完成。", path);
}
}
}
正如你所见,一个继承了IJob的TaskJob,将要执行的任务统统放在Execute中即可!
三、作业调度
创建一个WinForm项目,并添加对Quartz.dll和类库的引用。
JobConfig.xml
新建一个JobConfig.xml文件,指定TaskJob并配置Job的触发时间。
可以指定具体的执行时间,如<cron-expression>0 50 9 ? * *</cron-expression> 表示每天9点50点触发。
也可以指定为一段时间内重复多少次,如 <cron-expression>0 0/1 8-20 ? * MON-FRI</cron-expression> 表示周一到周五每天的8点到20点,每一分钟触发一次。
<quartz xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0"
overwrite-existing-jobs="true">
<job>
<job-detail>
<name>MyJob</name>
<group>MyJob</group>
<job-type>MyJob.TaskJob, MyJob</job-type>
</job-detail>
<trigger>
<cron>
<name>cronMyJob</name>
<group>cronMyJob</group>
<job-name>MyJob</job-name>
<job-group>MyJob</job-group>
<!--周一到周五每天的8点到20点,每一分钟触发一次-->
<!--<cron-expression>0 0/1 8-20 ? * MON-FRI</cron-expression>-->
<!--每天9点50点触发-->
<cron-expression>0 50 9 ? * *</cron-expression>
</cron>
</trigger>
</job>
</quartz>
HandleMask
创建一个类,用来控制Job的开启和停止。
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using Quartz;
using Quartz.Impl;
using Quartz.Xml;
namespace WinApp
{
public class HandleMask
{
IScheduler sched;
public void Start()
{
JobSchedulingDataProcessor processor;
processor = new JobSchedulingDataProcessor(true, true);
ISchedulerFactory sf = new StdSchedulerFactory();
sched = sf.GetScheduler();
string path = AppDomain.CurrentDomain.BaseDirectory + "JobConfig.xml";
Stream s = new StreamReader(path, System.Text.Encoding.GetEncoding("UTF-8")).BaseStream;
processor.ProcessStream(s, null);
processor.ScheduleJobs(new Hashtable(), sched, false);
sched.Start();
}
public void Stop()
{
if (sched != null)
{
sched.Shutdown(true);
}
}
}
}
start的时候会根据JobConfig.xml中的配置调度TaskJob中的Execute。
控制器
控制器的主要功能是启动和停止对作业的调度。
初始化
在下面的构造函数中用到了 mutex 。可见参考使用 Mutex实现会话状态下单实例运行和系统范围内单实例运行 。
同时,在窗体初始化时,就启动了作业。
batch = new HandleMask();
batch.Start();
bool closeTag = true;
public Form1()
{
try
{
bool newMutexCreated = false;
string mutexName = "Global\\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
Mutex mutex = null;
try
{
mutex = new Mutex(false, mutexName, out newMutexCreated);
}
catch (Exception ex)
{
Environment.Exit(1);
}
if (newMutexCreated)
{
InitializeComponent();
lblCurState.Text = "作业已启动,启动时间:" + DateTime.Now;
batch = new HandleMask();
batch.Start();
}
else
{
Environment.Exit(1);
}
}
catch (Exception ex)
{
throw ex;
}
}
作业启动事件
{
batch.Start();
tscbStart.Enabled = false;
tscbStop.Enabled = true;
tscbExit.Enabled = true;
lblCurState.Text = "作业已启动,启动时间:"+DateTime.Now;
}
作业停止事件
{
batch.Stop();
tscbStart.Enabled = true;
tscbStop.Enabled = false;
tscbExit.Enabled = true;
lblCurState.Text = "作业已暂停,停止时间:"+DateTime.Now;
}
退出事件
{
batch.Stop();
closeTag = false;
Application.Exit();
}
辅助功能
禁止最大化:MaximizeBox:false;
禁止调整窗体大小: FormBorderStyle:FixedSingle
点击关闭时最小化
{
if (this.WindowState == FormWindowState.Minimized)
{
NormalToMinimized();
}
}
private void MinimizedToNormal()
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
private void NormalToMinimized()
{
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
this.notifyIcon1.Visible = true;
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.MinimizedToNormal();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (closeTag)
{
e.Cancel = true;
NormalToMinimized();
}
}
四、源码下载
源码。
-
- DeepSought
- 探索AI融入千行百业与日常生活
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构