Windows Service or Scheduled Task, which one do we prefer?

后端 未结 10 1813
时光取名叫无心
时光取名叫无心 2020-12-16 12:07

If we need to write a program that works periodically, which way do we prefer? Writing a windows service or writing a console application which works as scheduled task?

相关标签:
10条回答
  • 2020-12-16 12:41

    Alternatively have a look at quartz.net if you want something to run periodically. It's a pre-built framework that allows you to schedule tasks and saves you writing the service part yourself.

    0 讨论(0)
  • 2020-12-16 12:42

    "It depends", but I usually prefer scheduled tasks, for simplicity:

    • You do not need to add any boilerplate service code to you program.
    • Your program can be run from the command line for troubleshooting (you would have to use command line switches in your service program to accomplish this).

    If this is a more or less a one off, and something that you will install and controll yourself, a scheduled task is probably a good idea.

    For something that should have the feel of a finished product, and the customer should install themselves, I would go with a Windows Service.

    And the schedule is also an issue. The minimum schedule for a scheduled task is one minute. Anything below that, you either need to use a windows service, or build a loop into your job.

    0 讨论(0)
  • 2020-12-16 12:43

    Depends on just how regular you need it to run.

    If its something that needs to run every 60 seconds all day, I'd go with a Windows Service.

    If its something that only needs to run once a day, I'd go with Scheduled Task

    Anything in the middle... use your judgement :)

    0 讨论(0)
  • 2020-12-16 12:50

    I would think it would also depend on if you can leave the computer logged in or not. I've found that a Windows Scheduled Task cannot run unless the person that set up the task is logged in. If the computer cannot stay logged into, the program must be run as a service vice Scheduled Task.

    0 讨论(0)
  • 2020-12-16 12:56

    I think a service might be useful if you want to use WCF or .NET Remoting and have client applications communicating with some host service; otherwise I agree that a scheduled task is preferable to a service if the more complicated service adds nothing new.

    And @Tom, his statement about having to stay logged into a computer for a scheduled task to run is false. I just tested myself, and confirmed that a Windows schedule task will still run even if you're not logged in (unless of course you select the option that the task only runs while you're logged on).

    0 讨论(0)
  • 2020-12-16 13:00

    If it truly is "periodically" then I'd go with Scheduled Task. These can be set up to run at any desired frequency.

    Services are (in my experience) more for programs that have to respond to essentially random events, such as files arriving via FTP, or ones that have to monitor the state of the file system or database.

    0 讨论(0)
提交回复
热议问题