花了大约 30-35 小时找到解决方案后,我找到了 quartz.dll 解决方法。它在C# 中可用。使用 Quartz,我们可以非常轻松地安排或致电任何JOB/C# function。
我们只需要在 Global.asax 文件中的 Application_Start 事件中启动我们的工作。
为了更多的理解,你可以参考下面的代码,这对我来说是完美的!
Gloabl.asax:-
void Application_Start(object sender, EventArgs e)
{
SchedulerUtil schedulerUtil = new SchedulerUtil();
schedulerUtil.StartJob();
}
在 Class SchedulerUtil.cs 中:-
public void StartJob()
{
IScheduler iPageRunCodeScheduler;
string SCHEDULE_RUN_TIME = "05:00"; // 05:00 AM
// Grab the Scheduler instance from the Factory
iPageRunCodeScheduler = StdSchedulerFactory.GetDefaultScheduler();
TimeSpan schedularTime = TimeSpan.Parse(SCHEDULE_RUN_TIME);
iPageRunCodeScheduler.Start();
DbCls obj = new DbCls();
// define the job and tie it to our class
DateTime scheduleStartDate = DateTime.Now.Date.AddDays((DateTime.Now.TimeOfDay > schedularTime) ? 1 : 0).Add(schedularTime);
//IJobDetail job = JobBuilder.Create<Unity.Web.Areas.Admin.Controllers.CommonController.DeleteExportFolder>()
IJobDetail job = JobBuilder.Create<JobSchedulerClass>() // JobSchedulerClass need to create this class which implement IJob
.WithIdentity("job1", "jobGrp1")
.Build();
// Trigger the job to run now, and then repeat every 10 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "jobGrp1")
//.StartNow()
.StartAt(scheduleStartDate)
.WithSimpleSchedule(x => x
//.WithIntervalInHours(24)
.WithIntervalInSeconds(15)
.RepeatForever())
.Build();
// Tell quartz to schedule the job using our trigger
iPageRunCodeScheduler.ScheduleJob(job, trigger);
}
在 JobSchedulerClass.cs 中:-
public class JobSchedulerClass : IJob
{
public void Execute(IJobExecutionContext context)
{
Common obj = new Common();
obj.ScheduledPageLoadFunction();
}
}