Scheduled console app vs Windows service? When is it appropriate to use each

前端 未结 7 2002
误落风尘
误落风尘 2020-12-24 12:55

I just read this: What is the benefit of developing the application as a windows service? but I\'m still unsure of when to use a windows service.

I have a couple tas

相关标签:
7条回答
  • 2020-12-24 13:25

    For any scheduled task, I would usually recommend a Windows Service, for the following reasons:

    • A Windows Service will run even if a user is not logged into the PC (will run even if the server is sitting at the login prompt) (***Note - this may depend on the version of Windows you are running).
    • A service can run as high-authority accounts such as Network Service or Local System, or a User - they have more configurability in that regard
    • A service also comes built in with options for starting, stopping, restarting, and pausing while running (sometimes)
    • You can also set up failure conditions for services, like if it fails have it auto-restart

    As far as other examples of applications that can be windows services, a lot of times they are useful for applications such as remoting - you can have a service run a remoting server that clients connect to. Obviously very useful for data processing tasks that you want to run in the background as well, or processes where you want to send an email on certain conditions, etc.

    In general I've always found scheduled tasks to be much more fragile and unreliable. And unless you have them log properly, often harder to debug.

    In reference to the bug with the Timer - if you read the bug report on MS's site, you can see that it is caused when you call "Stop" inside the Timer_Elapsed event. The answer to this is simple - don't call stop. Instead, wrap the whole thing in a check for a "IsRunning" boolean and only run if IsRunning is false. Even if there wasn't an issue with the timer, you need to do this anyway because the timer might re-fire during your execution if your execution takes longer than your timer interval.

    Anyway, I still think using scheduled tasks is a weak solution and gives me flashbacks of Windows 95.

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