I am currently developing a windows service in C# which is supposed to run a data import every day at configurable times. For this purpose I have created a class "ExecutionTime" which contains the attributes "Hour" and "Minute" to specify a time when the data import should be executed. When the service is started, the configured times are parsed into one ExecutionTime object each and those objects are then added to a list "executionTimes". I also created a class "Scheduler" which is responsible for checking if the data import should be executed and executing it once one of the configured times is reached.

I have already created a possible solution but I am not sure if my solution is considered "good practice" or if there are any edge cases when my code will not work the way it should. Could I therefore please get some feedback on my idea? Is it good? Can it still be improved?

My idea is to run the following code inside a while-loop:

DateTime now = DateTime.Now;

foreach (ExecutionTime executionTime in executionTimes)
    if (now.Hour == executionTime.Hour && now.Minute == executionTime.Minute
        && (lastExecutionTime.Hour < executionTime.Hour || lastExecutionTime.Minute < executionTime.Minute || lastExecutionTime < DateTime.Today))
        lastExecutionTime = now;


Please do not suggest using scheduled tasks as a solution because that is not up for discussion, I have to implement this in a windows service.

I have already asked this question on StackOverflow but someone recommended to ask it here instead.

Thanks a lot in advance!

  • \$\begingroup\$ Welcome to Code Review. How many ExecutionTime objects will be in executionTimes? \$\endgroup\$ – Heslacher Oct 24 '19 at 12:59
  • \$\begingroup\$ @Heslacher Thank you! Currently there will be four objects in it. There is a possibility that more will be added in the future but I doubt it. \$\endgroup\$ – Chris Oct 24 '19 at 13:27
  • 1
    \$\begingroup\$ Take a look at quartz-scheduler.net it can be adjusted to your needs. Running inside a service won't be a problem either. If you click on the right side on "Documentation" you will see a "Getting Started" as well. \$\endgroup\$ – Heslacher Oct 24 '19 at 13:56
  • \$\begingroup\$ @Heslacher Thank you for the idea! However, I'd like to avoid including yet another whole library into my project just to run my code at a few given times per day. I know that it is usually not a good idea to re-invent the wheel but this problem seemed simple enough to me that I can do it myself in a few lines of code. Could I please get some feedback regarding the code that I provided? \$\endgroup\$ – Chris Oct 24 '19 at 14:14
  • \$\begingroup\$ It can start as a very simple task and evolve just so little that it can get messy to deal with while extending it. I definitely agree that you should use a well tested and a stable/reliable library if you can. Apart from Quartz.Net you can also take a look at hangfire.io. \$\endgroup\$ – Coda Oct 25 '19 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.