Reputation: 551
Can a windows service be set to run at a specific time..?
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
Upvotes: 2
Views: 6763
Reputation: 4606
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.
Example Start Service:
net start MyService
Example Stop Service
net stop MyService
Upvotes: 0
Reputation: 3420
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.
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
}
Upvotes: 2
Reputation: 15931
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.
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.
Upvotes: 2
Reputation: 564323
Typically, if this is your requirement, you would be better served by writing a simple console application, and then using the Windows Task Scheduler to schedule it as needed.
This will provide the same benefits as a service, but allow you a lot more control over scheduling after deployment.
Upvotes: 6