8

I created a Timerjob scheduled on a daily basis. But our requirement is to run the timer job every four hours. How can I achieve this? Someone give me a solution for this.

 string strJobTitle = "SAMPLE";
 JobStatusUpdate.Title = strJobTitle;
 SPSchedule dailySchedule = SPSchedule.FromString("daily at 00:00:00");
 JobStatusUpdate.Schedule = dailySchedule;
 JobStatusUpdate.Update();

This is will run daily 12 AM. How can I set to run every 4 hours ?

2 Answers 2

6

You cannot configure a timer job like that. The best approach you can go for is:

  • Configure the timer job to use an hourly schedule (run every hour)
  • At the start of timer job check if DateTime.Now.Hour is divisible by 4.
  • If not, exit the job, if yes - continue.

To set up a timer job to use hourly schedule, use the following code:

string strJobTitle = "SAMPLE";
JobStatusUpdate.Title = strJobTitle;
SPHourlySchedule schedule = new SPHourlySchedule();
schedule.BeginMinute = 0;
schedule.EndMinute = 5;
JobStatusUpdate.Schedule = schedule;
JobStatusUpdate.Update();

This code will configure to run the timer job every hour at the random time between the 1st and 5th minute of the hour.

You can check if the current hour is divisible by 4 with the following code:

(DateTime.Now.Hour % 4 == 0)

This will be true when current hour is 4, 8, 12, 16, 20, and 24 (if there is no '24', then add additional check for '0' hour). Put this as a condition in your timer job's Execute method. If it's true, you continue with the timer job, if it's not - you exit.

SPMinuteSchedule might work for you as well - you configure to run the job every 59 minutes, but you would still need to check what's the current hour.

Any of these two approaches you take, you will have an advantage of saving your timer job configuration (how often should the timer job run) in a list, or WebApp properties, etc.

2
  • int time = DateTime.Now.Hour; var Result = time / 4; If any value comes in time it will divisible by 4.how can i check can you please elaborate your answer.
    – Elina
    Aug 10, 2016 at 10:24
  • 1
    Try using condition if(time%4==0) then run the job else exit. Aug 10, 2016 at 10:36
1

Option 1:

Hope the reference link below will give you some idea.

https://social.msdn.microsoft.com/Forums/en-US/7947d637-dedd-468d-9be3-42addb18c566/how-to-set-the-timer-job-every-one-hour-daily-except-sunday

This is for running the job every one hour. But you can tweak the solution to make it to run for every 4 hours.

Option 2:

Alternately you can try the code below

SPHourlySchedule scheduleHour = new SPHourlySchedule();
scheduleHour.BeginMinute = 1;
scheduleHour.EndMinute = 5;
timerJob.Schedule = scheduleHour;
timerJob.Update()

You can use the SPHourlySchedule class which would schedule your timer job to run every hour. But if you want your timer job to run every 4 hours, then in the execute method of your timer job you need to put an "if" condition to check if the current hour is a divisible of 4.

4
  • Please add details to your answer, it is of no help if the link breaks Aug 10, 2016 at 9:31
  • every one hour is not a problem for that i can use SPMinuteshedule but every 4 hour is a problem i am not able to use SPMinuteshedule and SPdailyshedule.
    – Elina
    Aug 10, 2016 at 9:47
  • The link is dead now.
    – Uwe Keim
    Sep 8, 2017 at 6:44
  • Updated the dead link in my reply. Please refer this correct link: social.msdn.microsoft.com/Forums/en-US/…
    – Saravanan
    Oct 6, 2017 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.