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.