由於專案中要運用到此排程方法,所以抽空學習了下,簡單的用法可以掌握了
首先作為沒有用過的人來說,怎麼用呢?
於是百度了下原來先要下載
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 + "");
}
}
注意這裡是建的另一個類
看著還簡單撒: