Changhui Xu
1 min readApr 14, 2020

--

Hi Ryan Tauriainen, in the current code setup, if your job takes more than 1 min to finish, and you schedule it to run every minute, then the job will skip the minutes when it is running. For example, if the job takes 90 seconds to finish, then it will actually run at every two minutes, because it checks the next schedule only after the job (current run) finishes. The existing code in my article and my GitHub repo will not throw exception in this case.

From your exception message, the error might raised from new System.Timers.Timer(delay.TotalMilliseconds). Could you debug this line? I couldn’t think of a reason why the delay gives a negative value here, unless the job is so lucky that the clock happens to miss several ticks. If this is the case, could you add a guard to this line, like the following snippet?

if (delay.TotalMilliseconds < 0)
{
await ScheduleJob(cancellationToken);
}
else
{
_timer = new Timer(delay.TotalMilliseconds);
}

BTW: It is also a good practice to handle possible exceptions inside the “DoWork()” method, just in case something is the actual job breaks outside loop.

--

--

Changhui Xu
Changhui Xu

Written by Changhui Xu

Lead Application Developer. MBA. I write blogs about .NET, Angular, JavaScript/TypeScript, Docker, AWS, DDD, and many others.

No responses yet