How to schedule a C# Windows Service to run a method daily?

How to schedule windows service in c#
Scheduling Window Service Daily,Weekly,Monthly ?
Creating Scehduler Service using Windows Services.
How to schedule windows service in c#?
How to schedule a C# Windows Service to run a method daily?

To make a windows service do the job at particular time of the day (just like a task scheduler) we need to set the timer and calculate the interval to schedule the event.

I have a requirement to create Windows service and schedule it, this article will not cover everything about windows service, we are covering scheduling the service using using System.Timers;

MSDN states that System.Timers “allows you to specify a recurring interval at which the Elapsed event is raised in your application. You can then handle this event to provide regular processing.”

Timers Properties

These are notes on properties, methods and events from the Timer class in the .NET Framework. As shown above, you need to add the System.Timers namespace at the top of your file for easy access to Timer.

Timer.AutoReset:
Indicates “whether the Timer should raise the Elapsed event each time the specified interval elapses.”

Timer.Enabled:
MSDN: “Whether the Timer should raise the Elapsed event.” You must set this to true if you want your timer to do anything.

Timer.Interval:
This indicates “the time, in milliseconds, between raisings of the Elapsed event. The default is 100 milliseconds.”

Timer.Start:
This does the same thing as setting Enabled to true. It is unclear why we need this duplicate method.

Timer.Stop:
This does the same thing as setting Enabled to false. See the Timer.Start method shown previously.

Timer.Elapsed Event:
An event that is invoked each time the Interval of the Timer has passed. You must specify this function in code.

basically we are creating object timer, Set Timer Interval, and create a delegate ElapsedEventHandler, Hook up the Timer.Elapsed

As below

            Timer timer = new Timer();
            timer.Interval = 1000 * 60 * 60 * 24;//set interval of 1 day
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

Where parameter to event handler “timer_Elapsed” is name of function which will execute after defined time interval
And finally call
timer.Start();
to start the timer which will start the timer

Here is full code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using EPocalipse.IFilter;
using System.Timers;

namespace WinServ1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        static Timer timer;

        protected override void OnStart(string[] args)
        {
            timer = new Timer();
            timer.Interval = 1000 * 60 * 60 * 24;//set interval of one day 
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            start_timer();
            Console.Read();
        }

        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Add your code here

        }
        private static void start_timer()
        {
            timer.Start();
        }

        protected override void OnStop()
        {
        }
    }
}

Advertisements
This entry was posted in C#.NET and tagged . Bookmark the permalink.

One Response to How to schedule a C# Windows Service to run a method daily?

  1. sonal says:

    Nice article on scheduling windows service

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s