由于项目中要运用到此调度方法,所以抽空学习了下,简单的用法可以掌握了

首先作为没有用过的人来说,怎么用呢?

于是百度了下原来先要下载

quartz.net2.0,

然后我就去下了个quartz.net2.0

,接着便是要把Quartz.dll还有Common.Logging.dll引用了

,最后就是把config配置了,这个简单:

直接贴代码

<configSections>

<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />

</configSections>

<quartz>

<add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler" />

<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />

<add key="quartz.threadPool.threadCount" value="10" />

<add key="quartz.threadPool.threadPriority" value="2" />

<add key="quartz.jobStore.misfireThreshold" value="60000" />

<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />

</quartz>

这些准备工作做好了,就到主菜了

public void Send_Saveinfo()
{
//获取架构接口信息
IScheduler scheduler = factory.GetScheduler();
///开启Quartz.net的架构
scheduler.Start();
//找到WriteLogJob这个作业
IJobDetail job = JobBuilder.Create<WriteLogJob>().WithIdentity("SampleJob", "JobGroup1").Build();
//什么时间执行该作业
//ITrigger trigger = TriggerBuilder.Create().StartNow().Build();
///每日0点的时候执行此方法
ITrigger trigger = new CronTriggerImpl("CronTrigger", "TriggerGroup1", "0 0 0 * * ?");
scheduler.ScheduleJob(job, trigger);

}

///每日0点的时候执行此方法
ITrigger trigger = new CronTriggerImpl("CronTrigger", "TriggerGroup1", "0 0 0 * * ?");
scheduler.ScheduleJob(job, trigger);

这里是你的winform或者wpf要用的方法

最后注意的是当你退出这个页面的时候要加的方法

//当要关闭程序的时候要停止这个服务
IScheduler scheduler = factory.GetScheduler();
if (scheduler != null)
{
scheduler.Shutdown(true);
}

如果不加这个的话,再次打开的话就会报错了,除非在进程里面把这个服务停止了

当然这里只是执行页面,由上面我们要建一个WriteLogJob这个作业,其对应的名称当然也是WriteLogJob

但是要对应其接口:IJob

如下:

public class WriteLogJob : IJob
{

private readonly EcoCementDataBasic.Data.OrderInformationData tempOrderData = new OrderInformationData();
public void Execute(IJobExecutionContext context)
{
//获取当前文件路径
// string fileLogPath = AppDomain.CurrentDomain.BaseDirectory;

//保存当前路径下订单信息excel
List<EcoCementDataBasic.Data.OrderModel> tempOrderInformation = tempOrderData.ExcelModel();
StreamWriter sw = null;
SaveFileDialog sfd = new SaveFileDialog();
//指定路径
string path = SaveConfig.GetConfigValue("ReportFilepath")+"//";

sfd.FileName = "订单信息" + DateTime.Now.ToString("yyyyMMdd") + ".xls";

try
{
sw = new StreamWriter(path+sfd.FileName.ToString(), false, Encoding.Unicode);
string sb = GetGridDetailTableHtml(tempOrderInformation, 9, "订单信息", DateTime.Now.ToShortDateString());
sw.Write(sb);
sw.Flush();
Program.Get_ILog().Log(string.Format("成功导出报表信息至服务器"));

}
catch (IOException ioe)
{
throw ioe;

}
finally
{
if (sw != null)
{
sw.Close();

}
}

//获取路径文件
string file = path + sfd.FileName;

//获取发送邮件的配置信息
LoadConfig();
SendEmail sendMail = new SendEmail();
sendMail.SendMail(ToEmail, FromEmail, "这是当前订单实时余量信息", "订单信息"+DateTime.Now.ToString("yyyy-MM-dd"), FromPwd);
sendMail.Attachments(""+file+ "");
sendMail.SendAsync(SendCompletedCallback, FromSMTPServer, FromSMTPPort);
//sendMail.Send(FromSMTPServer, FromSMTPPort);

//删除1周前的数据
sfd.FileName = "订单信息" + DateTime.Now.AddDays(-7).ToString("yyyyMMdd") + ".xls";
if (File.Exists(@"" + sfd.FileName + ""))
{
//如果存在则删除
File.Delete(@"" + sfd.FileName + "");
}

}

注意这里是建的另一个类

看着还简单撒:

05-11 19:57