Have a look at ncrontab project.
(You can either manually add a DLL reference to your solution or simply install it with NuGet.)
ncrontab allows for easy configuration of task like yours.
In your code or App.config you simply define a string that consists of 5 parts:
* * * * *
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
To make it a little simpler: Would it be possible to start both tasks at the same time? AFAIK it is not possible to confige individual times for each day within the same pattern.
string pattern = "* 17 * * 4,5";
If this is not practical for you, you could define two different patterns and then check which one of them occurs sooner.
In your case this might look like this:
// Friday 1 PM
string fridayPattern = "* 13 * * 5";
or
// Thursday 5 PM
string thursdayPattern = "* 17 * * 4";
The code to interpret the patterns could look like this:
CrontabSchedule schedule = CrontabSchedule.Parse(pattern);
DateTime nextOccurrence = this.schedule.GetNextOccurrence(DateTime.Now);
TimeSpan interval = nextOccurrence.Subtract(now);
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = interval.TotalMilliseconds;
timer.Elapsed -= this.OnScheduleTimerElapsed;
timer.Elapsed += this.OnScheduleTimerElapsed;
timer.Start();
Note:
If you are using the 2 pattern idea, you would CrontabSchedule.Parse() both strings (fridayPattern and thursdayPattern) here and then find which GetNextOccurrence() would occur sooner.
Either way, then, of course, you would need to handle the event:
private void OnScheduleTimerElapsed(object sender, ElapsedEventArgs e)
{
this.timer.Stop();
this.timer.Elapsed -= this.OnScheduleTimerElapsed;
// Start your work here.
}
Your second question:
To configure multiple runs on the same day would look like this:
// Friday, 1:00 PM, 3:00 PM, 6:00 PM
string multipleRunsOnSameDayPattern = "* 13,15,18 * * 5"