I was wondering if the following code snippet is the correct way:
Logger.Debug("Check file exists for corresponding path: {0}.", filePath);
var timeDeadline = DateTime.Today + Settings.Default.FileDeadline; //Timespan type
while (!File.Exists(filePath))
{
if (DateTime.Now > timeDeadline)
Logger.Error("The file is not available: {0}.", filePath);
Logger.Info("The file is not yet available. Waiting for 1 hour...");
Thread.Sleep(TimeSpan.FromHours(1));
}
The purpose of this code is to schedule it early in the morning with the Windows Task Scheduler to load a file that comes normally around 9:00 but can have delays of hours and at the end is needed before 14:30 (FileDeadline) when I will report the missing file as an error if still missing.
I could also throw an exception very hour and allow windows task to restart the process every hour but I thought better to let C# have all the control of the process. The disadvantage is the thread sleeping one hour and having a scheduled task running hours. I don't mind if the resources used are negligible but I though some more experienced programmers might have a more elegant solution.