1

I have created a console program that transfers data from a database to a server. This program must run every day at a certain time. At first I thought of a scheduled task but the problem is that it depends on a user being logged in. However, as it is rather tricky and the service has to run every day, I came to the Windows service through my research.

However, most of the information comes from 2009 and 2010 and may already be outdated? I also found a guide explaining how to create a scheduled service:

https://www.aspsnippets.com/Articles/Simple-Windows-Service-that-runs-periodically-and-once-a-day-at-specific-time-using-C-and-VBNet.aspx

Is that still best practice to do it that way? What would you recommend?

Rufus L
  • 36,127
  • 5
  • 30
  • 43
user3772108
  • 854
  • 2
  • 14
  • 32
  • 2
    Why do you think a scheduelled task can only be run when a user is logged in? That is only required for things that interact with the UI, but if you where going to make it a service you would not be able to interact with the UI anyway. – Scott Chamberlain Dec 06 '17 at 16:04
  • 2
    Your dangling participle is showing. Are you saying your program requires a user being logged in or that scheduled tasks require a user being logged in? The latter isn't true. If you're talking about the former, I don't see how a Service solves the problem. @ScottChamberlain beat me by seconds! – itsme86 Dec 06 '17 at 16:05
  • In what way does it depend on a login? You can configure the scheduled task to run under a specific user? – usr Dec 06 '17 at 17:14
  • 1
    FYI, the guide you linked to gives you an idea of how you can implement this in a service, but be aware that the code has quite a few little quality issues. In any case, you can do either a scheduled task or a windows service. Which is "better" is a matter of opinion, I suppose, which is why I'm voting to close this as opinion-based. – Rufus L Dec 06 '17 at 18:39
  • @ScottChamberlain We had problems in the past with a server where such a task was running. If all users where logged-out the task wasn't started. A source: https://stackoverflow.com/questions/507674/scheduled-console-app-vs-windows-service-when-is-it-appropriate-to-use-each quote "A Windows Service will run even if a user is not logged into the PC" – user3772108 Dec 07 '17 at 08:48
  • @RufusL Thanks for the hint with the code. I don't think it is opinion-based since the answer below helped me to understand which direction I need to go. – user3772108 Dec 07 '17 at 08:57

1 Answers1

2

Your first thought was right, use a scheduled task and select the 'Run whether user is logged on or not'.

If you use a Windows Service you have to code the trigger yourself which could be fraught with errors.

If you'd like something more 'enterprise' I'd suggest looking at Quartz.NET. It's an open source scheduling library, although it's probably overkill for what you're trying to achieve.

Jynx
  • 90
  • 4