C#开发作业调度软件讲解

本文和大家介绍一下比较实用的作业调度小软件,在一些场合使用起来很方便能做起实用的事情。


一、效果图


作业启动状



作业停止状态



二、Quartz


调度的核心库用的是 Quartz.NET。官方网站:http://quartznet.sourceforge.net/。这里有更多的中文介绍。


先创建一个类库,新建一个TaskJob类用来执行任务。



<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> using System;
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点,每一分钟触发一次。



<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> <? xml version="1.0" encoding="utf-8" ?>
< 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的开启和停止。



<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> using System;
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();



<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->         HandleMask batch;
       
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;
            }
        }


作业启动事件



<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->         private void tscbStart_Click( object sender, EventArgs e)
        {
            batch.Start();
            tscbStart.Enabled
= false ;
            tscbStop.Enabled
= true ;
            tscbExit.Enabled
= true ;
            lblCurState.Text
= " 作业已启动,启动时间: " + DateTime.Now;
        }


作业停止事件



<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> private void tscbStop_Click( object sender, EventArgs e)
        {
            batch.Stop();
            tscbStart.Enabled
= true ;
            tscbStop.Enabled
= false ;
            tscbExit.Enabled
= true ;
            lblCurState.Text
= " 作业已暂停,停止时间: " + DateTime.Now;
        }


退出事件



<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->         private void tscbExit_Click( object sender, EventArgs e)
        {
            batch.Stop();
            closeTag
= false ;
            Application.Exit();
        }


辅助功能
禁止最大化:MaximizeBox:false;
禁止调整窗体大小: FormBorderStyle:FixedSingle


点击关闭时最小化



<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> private void Form1_Resize( object sender, EventArgs e)
        {
            
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();
           }
        }


四、源码下载
源码


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值