简体   繁体   English

如何按计划启动和停止Windows服务?

[英]How to start and stop a windows service on a schedule?

I have a service that I have written to perform batch duties, but a requirement is to start the processing at 4:00pm and end its processing at 5am. 我有一个我写的服务来执行批处理职责,但要求是在下午4点开始处理并在凌晨5点结束处理。 If there is a job still processing, the time would need to be extended until the job is finished. 如果仍有作业处理,则需要延长时间直到作业完成。

Would I wrap the onstart call in a timer or what? 我会在计时器中包装onstart电话还是什么? I really dont know what this would look like as I am used to services starting and running until infinity (or until the box tanks.. or an unhandled exception is met). 我真的不知道这会是什么样子,因为我习惯于服务开始和运行直到无穷大(或者直到盒子坦克......或者遇到未处理的异常)。

I would definitely appreciate some ideas on how to achieve this. 我肯定会欣赏有关如何实现这一目标的一些想法。

Edit: I do need a service because I am executing an SSIS package through dtexec() and a batch file in C#. 编辑:我确实需要一个服务,因为我正在通过dtexec()和C#中的批处理文件执行SSIS包。 Some other things have to happen as well like logging to a dbase and queue management. 其他一些事情也必须发生,例如记录到dbase和队列管理。

Each job has a varying time length, some will complete in a few minutes, others make take 5 hours. 每个工作的时间长度各不相同,有些会在几分钟内完成,有些则需要5个小时。

This sounds like a task that is better handled by the Windows Task Scheduler. 这听起来像是Windows任务计划程序更好地处理的任务。 Unless you're running something like Win2K or XP, it has all the settings necessary to start at a certain time, allow it to run for a limited amount of time, it can start the process under specific credentials (if you need it to run elevated), as well as run when no one is logged on. 除非您运行的是Win2K或XP,否则它具有在特定时间启动所需的所有设置,允许它在有限的时间内运行,它可以在特定凭据下启动该进程(如果您需要它运行)升级),以及没有人登录时运行。

And it has a nice handy GUI for managing it all, no coding necessary! 它有一个非常方便的GUI来管理它,无需编码!

UPDATE UPDATE

OK, based on your comments, what you really need is a workflow. 好的,根据您的评论,您真正需要的是工作流程。 What I would do, is develop each of the individual tasks in a seperate project (seperate .exe, or .dll depending on your needs). 我要做的是,在单独的项目中开发每个单独的任务(单独的.exe或.dll,具体取决于您的需要)。 And then look and Windows Workflow Foundation and develop a workflow "controller" that you can use to run the individual tasks. 然后查看Windows Workflow Foundation并开发一个工作流“控制器”,您可以使用它来运行各个任务。

我认为该服务应该运行到无穷大并检查时间,看看是否已经是下午4:00,如果是的话,开始它的工作,当凌晨5点发生时,只需完成并再次下午4:00。

what you're looking for is a scheduled job 你要找的是预定的工作

if you /must/ have a windows service, you use Timers and set the timer to check once every (whenever, I use 30 minutes for this sort of thing) and if the time is between 4am and 5am, do work. 如果你/必须/有一个Windows服务,你使用计时器并设置计时器每次检查一次(每当我用这种东西30分钟),如果时间是凌晨4点到凌晨5点,那就行了。

你可以看一下quartz.net ,它是一个很好的调度库,可以完成这类工作,可以在服务中运行。

Three options: 三种选择:

  1. You could use Windows task scheduler and start the task at the designated time and then keep track within the program how long the task has been running. 您可以使用Windows任务计划程序并在指定时间启动任务,然后在程序中跟踪任务运行的时间。

  2. Have the service constantly "running" but sleeping for a certain amount of time between executions. 使服务不断“运行”,但在执行之间休眠一段时间。 This would lead to problems if the program ran longer than anticipated, however. 但是,如果程序运行时间超过预期,则会导致问题。

  3. Set the service to run at 15 minutes (or so) intervals and check to see what time it is. 将服务设置为以15分钟(左右)的间隔运行,并检查它是什么时间。 If it is during your starting period, start the program. 如果是在您的开始阶段,请启动该程序。

The first and 3rd seem like the best options. 第一和第三似乎是最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM