如果要在asp.net中實作類別似windows中計劃任何的功能,你會怎麼做?
您可以在留言裡寫出您的方法,以便我學習和改進自己的程式,謝謝。
以下是我的方法;
首先下載Quartz.net
web.config加入以下兩個片段
代碼
<configSections>
<!--Quartz-->
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<!--End Quartz-->
</configSections>
代碼
<!--Start Quartz-->
<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>
<!--End Quartz-->
Global.asax加入以下片段
代碼
void Application_Start(object sender, EventArgs e)
{
//Sys.Log.Sys.Info("starting sched...");//日誌中計入系統開始時間
// construct a scheduler factory
ISchedulerFactory schedFact = new Quartz.Impl.StdSchedulerFactory();
// get a scheduler
sched = schedFact.GetScheduler();
sched.Start();
// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(Task.QuartzJob_Test));
// fire every hour
Trigger trigger = TriggerUtils.MakeSecondlyTrigger(60 * 5);
// start on the next even hour
trigger.StartTimeUtc = DateTime.UtcNow;
trigger.Name = "myTrigger";
sched.ScheduleJob(jobDetail, trigger);
//delay iisPool
Task.DelayIISThreadPool.url = HttpContext.Current.Request.Url.ToString();
JobDetail jobDetail2 = new JobDetail("myJob2", null, typeof(Task.DelayIISThreadPool));
Trigger trigger2 = TriggerUtils.MakeSecondlyTrigger(60 * 10);
trigger2.StartTimeUtc = DateTime.UtcNow;
trigger2.Name = "myTrigger2";
sched.ScheduleJob(jobDetail2, trigger2);
// End Quartz
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
//Quartz
if (sched != null)
{
sched.Shutdown(true);
}
//End Quartz
}
App_Code裡加入以下兩個檔案
代碼
//QuartzJob_Test.cs
using System;
using System.Collections.Generic;
using System.Web;
namespace Task
{
/// <summary>
/// Summary description for DelayIISThreadPool
/// </summary>
public class DelayIISThreadPool : Quartz.IJob
{
#region IJob Members
public static string url;
private static System.Net.WebClient wc = new System.Net.WebClient();
public void Execute(Quartz.JobExecutionContext context)
{
if (string.IsNullOrEmpty(url))
return;
wc.DownloadString(url);
}
#endregion
}
}
代碼
//DelayIISThreadPool.cs
using System;
using System.Collections.Generic;
using System.Web;
namespace Task
{
public class DelayIISThreadPool : Quartz.IJob
{
#region IJob Members
public static string url;
private static System.Net.WebClient wc = new System.Net.WebClient();
public void Execute(Quartz.JobExecutionContext context)
{
if (string.IsNullOrEmpty(url))
return;
wc.DownloadString(url);
}
#endregion
}
}