我必须在隔日使用自定义计时器作业执行一些操作下面是创建和启动计时器作业的代码(在功能接收器中),但它每天运行,并且我必须每隔一天运行它?
怎么做?
private bool CreateJob(SPSite站点){bool jobCreated=false;尝试{
TimerJob job = new TimerJob(JobName, site.WebApplication);
job.Title = JobName;
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 15;
job.Schedule = schedule;
job.Update();
}
catch (Exception)
{
return jobCreated;
}
return jobCreated;
}
我认为上面的代码每15分钟运行一次,您有SPMinuteSchedule时,您必须使用SPDailySchedule。参见msdn文档
然而,当Job运行时,您可以获得当前的DayOfWeek,并在需要时运行您的代码
switch (DateTime.Now.DayOfWeek)
{
case DayOfWeek.Friday:
break;
case DayOfWeek.Monday:
break;
case DayOfWeek.Saturday:
break;
case DayOfWeek.Sunday:
break;
case DayOfWeek.Thursday:
break;
case DayOfWeek.Tuesday:
break;
case DayOfWeek.Wednesday:
break;
default:
break;
}
或者,您可以将上次运行保存在Job属性包中。在此链接中,您可以找到SPWeb的示例,但SPJob的示例相同。