How do I run a windows service at a specific time?
4 answers
- Change your service to remove the timer/sleep and call readDataFromAd() from a thread started from OnStart() (discussion of why a thread may be necessary here)
- Create a task in Task Scheduler that runs the following command at 11 pm: NET START Your-Service-Name.
Table of Contents
What is a Windows service application?
Microsoft Windows services, formerly known as NT services, allow you to create long-running executable applications that run in their own Windows sessions. These services can be started automatically when the computer boots, can be paused and restarted, and do not display any user interface.
When to use Windows service or Task Scheduler?
if it’s one in a day, why don’t you use task scheduler? Windows service is useful when you want to run a task many times per minute. so if you want to run a program at a specific time, it is better to use the task scheduler and set the task scheduler event to a specific time of day.
How to run a service at a specific time?
If DateTime.now is later than ScheduleTime, it will be negative and this will throw an exception when assigning to timer.Interval. Then do the subtraction. Are you sure you need a service that runs only once a day? Maybe Windows Task Schedule is a better solution?
How to create a windows service that runs periodically?
Daily mode: where the Windows Service will execute a task at a specific (certain) time of day. In this article, I will explain how to create a simple Windows service that runs periodically at regular intervals and also once a day at a specific (certain) time of day using C# and VB.Net.
How to execute a function in the specified time?
(1) On first start, set _timer.Interval to the number of milliseconds between the start of the service and the scheduled time. This example sets the schedule time to 7:00 am as _scheduleTime = DateTime.Today.AddDays(1).AddHours(7); (2) In Timer_Elapsed, reset _timer.Interval to 24 hours (in milliseconds) if the current interval is not 24 hours.