在特定时间运行 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8004150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run windows service on a specific time
提问by Link
Can a windows service be set to run at a specific time..?
是否可以将 Windows 服务设置为在特定时间运行..?
Example
例子
From 8:00 am to 5:00pm run process
From 5:00 pm to 8:00am run process
This process can run daily, weekly or a single day..
这个过程可以每天、每周或一天运行。
Please let me know, your thoughts
请让我知道你的想法
回答by Reed Copsey
Typically, if this is your requirement, you would be better served by writing a simple console application, and then using the Windows Task Schedulerto schedule it as needed.
通常,如果这是您的要求,您最好编写一个简单的控制台应用程序,然后使用Windows 任务计划程序根据需要安排它。
This will provide the same benefits as a service, but allow you a lot more control over scheduling after deployment.
这将提供与服务相同的好处,但允许您在部署后更多地控制调度。
回答by Sajib Mahmood
I faced a similar situation where I had to develop a service which would do something only during trading time which is from 11:00 am to 3:00 pm everyday in that specific case. It would remain idle the rest of the day. I am sharing what I did in my case. I am not sure whether it would be helpful for you but I hope that it will be.
我遇到了类似的情况,我不得不开发一项服务,该服务仅在特定情况下每天上午 11:00 至下午 3:00 的交易时间执行某些操作。它会在一天剩下的时间里闲置。我正在分享我在我的案例中所做的事情。我不确定它是否对您有帮助,但我希望它会有所帮助。
Two variables maintaining the starting time and ending time
维护开始时间和结束时间的两个变量
TimeSpan StartingTime = new TimeSpan(
Int32.Parse(
ConfigurationManager
.AppSettings["StartingTimeHour"]),
Int32.Parse(
ConfigurationManager
.AppSettings["StartingTimeMinute"]), 0);
TimeSpan EndingTime = new TimeSpan(
Int32.Parse(
ConfigurationManager
.AppSettings["EndingTimeHour"]),
Int32.Parse(
ConfigurationManager
.AppSettings["EndingTimeMinute"]), 0);
//Starting time and Ending time may change in future. So I used app.config
//instead of fixing them to (11,0,0) and (15,0,0) respectively
A method to check whether it is trading time
一种检查是否是交易时间的方法
public bool TradingTime()
{
TimeSpan CurrentTime = DateTime.Now.TimeOfDay;
return ((CurrentTime > StartingTime) && (CurrentTime < EndingTime));
}
Then where it is necessary to check the time and execute something depending on that:
然后有必要检查时间并根据它执行某些操作:
while (TradingTime())
{
//do whatever required
}
回答by Jason
There are a couple of ways to go about this. The easiest from a coding perspective is to write a console application and then run it using the task scheduler.
有几种方法可以解决这个问题。从编码的角度来看,最简单的方法是编写一个控制台应用程序,然后使用任务调度程序运行它。
A windows service is always running, so you would want your service to sleep for some amount of time, wake up check if the current time is in the window, and if it is then execute.
Windows 服务始终在运行,因此您希望您的服务休眠一段时间,唤醒检查当前时间是否在窗口中,然后执行。
Both approaches have their pros and cons, and it's really more about what the service does. One question I don't know the answer to is whether scheduled tasks run if the user that scheduled the task isn't logged in. This would be my biggest concern regarding the task scheduler approach.
这两种方法都有其优点和缺点,而且更多的是关于服务的作用。我不知道答案的一个问题是,如果计划任务的用户未登录,计划任务是否会运行。这将是我对任务计划程序方法的最大担忧。
回答by Mas
What you can do is to use the Windows Task Scheduler and create a task to start the services, and another task to stop the services. All you need to do is create a batch file to start and stop the services.
您可以做的是使用 Windows 任务计划程序并创建一个任务来启动服务,以及另一个任务来停止服务。您需要做的就是创建一个批处理文件来启动和停止服务。
Example Start Service:
示例启动服务:
net start MyService
Example Stop Service
示例停止服务
net stop MyService