7

I am having the following Timer to run in .Net core Ihosted Service,

TimeSpan ScheduledTimespan;
string[] formats = { @"hh\:mm\:ss", "hh\\:mm" };
string strTime = Startup.Configuration["AppSettings:JobStartTime"].ToString();
var success = TimeSpan.TryParseExact(strTime, formats, CultureInfo.InvariantCulture, out ScheduledTimespan);
Timer _timer = new Timer(JobToRun, null, TimeSpan.Zero, ScheduledTimespan);

I'm using this specific overload,

public Timer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period);

But JobToRun is executing as soon as the control reached to it. How do I make it run at a specific time of the day on an everyday basis? Thanks in advance.

Sushant Yelpale
  • 860
  • 6
  • 19

1 Answers1

13

Following function returns parsed job run time

private static TimeSpan getScheduledParsedTime()
{
     string[] formats = { @"hh\:mm\:ss", "hh\\:mm" };
     string jobStartTime = "07:10";
     TimeSpan.TryParseExact(jobStartTime, formats, CultureInfo.InvariantCulture, out TimeSpan ScheduledTimespan);
     return ScheduledTimespan;
}

Following function returns the delay time from current time of the day. If current time of the day passed the job run time, appropriate delay will be added to job run like below,

private static TimeSpan getJobRunDelay()
{
    TimeSpan scheduledParsedTime = getScheduledParsedTime();
    TimeSpan curentTimeOftheDay = TimeSpan.Parse(DateTime.Now.TimeOfDay.ToString("hh\\:mm"));
    TimeSpan delayTime = scheduledParsedTime >= curentTimeOftheDay
        ? scheduledParsedTime - curentTimeOftheDay     // Initial Run, when ETA is within 24 hours
        : new TimeSpan(24, 0, 0) - curentTimeOftheDay + scheduledParsedTime;   // For every other subsequent runs
    return delayTime;
}

below timer will call the methodToExecute based on JobRunDelay everyday

_timer = new Timer(methodToExecute, null, getJobRunDelay(), new TimeSpan(24, 0, 0));
Sushant Yelpale
  • 860
  • 6
  • 19
  • Hi, Is it okay for you to update the entire service class. My service is not working as expected, unable to figure out. Will just cross with your service class. – DeSon Mar 09 '22 at 11:32