How Might I Schedule a C# Windows Service to Perform a Task Daily

Schedule a C# Windows Service to perform a task daily

You can find out the difference between current time and your desired schedule time and make the elapse interval accordingly. This way the event of timer will fire once at the sheduled time. Set the interval of time to 24 hours when it is elapsed.

private System.Timers.Timer _timer;    

private void SetTimer()
{
DateTime currentTime = DateTime.Now;
int intervalToElapse = 0;
DateTime scheduleTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 10,0,0);

if (currentTime <= scheduleTime)
intervalToElapse = (int)scheduleTime.Subtract(currentTime).TotalSeconds;
else
intervalToElapse = (int)scheduleTime.AddDays(1).Subtract(currentTime).TotalSeconds;

_timer = new System.Timers.Timer(intervalToElapse);

_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}

void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//do your work
_timer.Interval = (24 * 60 * 60 * 1000);
}

How might I schedule a C# Windows Service to perform a task daily?

I wouldn't use Thread.Sleep(). Either use a scheduled task (as others have mentioned), or set up a timer inside your service, which fires periodically (every 10 minutes for example) and check if the date changed since the last run:

private Timer _timer;
private DateTime _lastRun = DateTime.Now.AddDays(-1);

protected override void OnStart(string[] args)
{
_timer = new Timer(10 * 60 * 1000); // every 10 minutes
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start();
//...
}


private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// ignore the time, just compare the date
if (_lastRun.Date < DateTime.Now.Date)
{
// stop the timer while we are running the cleanup task
_timer.Stop();
//
// do cleanup stuff
//
_lastRun = DateTime.Now;
_timer.Start();
}
}

Windows service scheduling to run daily once a day at 6:00 AM

Here, you have 2 ways to execute your application to run at 6 AM daily.

1) Create a console application and through windows scheduler execute on 6 AM.

2) Create a timer (System.Timers.Timer) in your windows service which executes on every defined interval and in your function, you have to check if the system time = 6 AM then execute your code

ServiceTimer = new System.Timers.Timer();
ServiceTimer.Enabled = true;
ServiceTimer.Interval = 60000 * Interval;
ServiceTimer.Elapsed += new System.Timers.ElapsedEventHandler(your function);

Note: In your function you have to write the code to execute your method on 6 AM only not every time

Schedule Windows Service to run at 3:00am each day

AutoReset should be true in order to raise the event multiple times. Why did you set it to false?

See https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer.autoreset?view=net-5

But better is to use windows scheduler. Your code won't help in case of a reboot or restarted/stopped service.

How to execute Windows service at a specified time daily

You don't. You setup a scheduled task; services as (generally) for background running processes.



Related Topics



Leave a reply



Submit