I would like to run code conditionally based on the time of day. The code is within a while loop in several worker tasks that run throughout my programs lifetime. Performing the comparison on every loop iteration seems wasteful, is there a more efficient way to get this desired result?
The restate my question in code, I am asking if there is a more efficient to duplicate this functionality, perhaps using timers or some other scheduling mechanism:
while( workerNotCanceled )
{
var time = DateTime.Now;
if (time.Hour > 8 and time.Hour < 16)
DoWork();
}