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

后端 未结 10 1814
时光取名叫无心
时光取名叫无心 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 13:03

    I would suggest running the process as a scheduled task if you can, and writing a service only if you need to. Services are quite a bit more difficult to write (correctly), and if you're running your process on a schedule of any sort, you're much better off using the windows scheduler than attempting to build a scheduler of your own (within the servce).

    • The windows task scheduler is more efficient than your home-made scheduler will be
    • The windows task scheduler offers a number of options that you almost certainly won't attempt to replicate, but may find useful later on
    • If you build a service, you'd have to recompile the program if you want to change the logic that determines what conditions to run the task under. If you use the scheduler, you just flip some options and are done with it.

    If you're trying to decide between the two, then obviously using the Task Scheduler is a viable option. And if using the Task Scheduler is a viable option, then building a service is almost certainly the wrong choice.

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

    Start with a console app. Seperate the logic which would sit inside your loop-process-sleep loop, then you can actually switch between them easily - even in the same EXE.

    I've done this. We could call:

    ourservice.exe -console

    and it'd just run. Or

    ourservice.exe -install

    and it'll install as a service :)

    I'd go scheduled task in 99% of cases. If you need to run all the time, listen on ports, watch a folder (maybe - can be done every 10 seconds without a problem): then do it in a service. If all you do is wake up, do some processing (or not), and then go back to sleep: use the scheduler. It's easier, cleaner (memory management, esp if you are using COM objects, and REALLY if you are using MAPI), and the options (weekly, but not on tuesdays at 5pm) with the MS scheduler are better than you can write in the time..... which is NO time, as it already exists and is free

    Oh, and it's easier to debug a console app (scheduler) than a service.... :) Or have someone "just run it".

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

    Avoid a service unless you must use it.

    It is a process - which means it runs all the time. Gratitious use of system resources is bad practise.

    I personally find it insulting that other people have their process executing 24/7 on MY computer when it is entirely unnecessary. It's MY performance they're using up. I disable all non-necessary background services.

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

    It depends on what you mean with periodically? Each second, Each minute, each hour, each day?

    I would say the more frequent a task is to be running, the more a Windows Service is preferred.

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