Need to Run the Windows Service on monthly basis

I Can i have a sample code to run the Windows service on monthly basis
Who is Participating?
Pratima PharandeCommented:
You can simply do it using Windows Task scheduler.Follow thw below steps
1. Goto the Scheduled Tasks. Double click on Add Scheduled Task.
2. A wizard will open up. Use it to schedule a job as usual.
3. Once the schedule is added, right click on it and goto properties.
4. In the Task tab, in the Run area provide the below command      net start "<windows service name>"
5. Click on OKIf you want to schedule for stoping a service use      net stop "<windows service name>"
for restarting      
net restart "<windows service name>"

Umashankar_GunaAuthor Commented:
Hi but my project requirement is that we need to achive this in windows service can you help me on that.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Jacques Bourgeois (James Burger)PresidentCommented:
Is the computer restarted regularly, or is it a machine that is left on all the time.

If it is restarted regularly, simply set the service for an automatic Start.

In the OnStart event, check for date you have saved somewhere. If the date is one month old or more, run the service and update the saved date.

If the date is less than one month, stop the service.

I would do it like this.

1. add a timer to your service class with an interval of a day
2. when the timer elapses,
2.1 turn the timer off
2.2 see if today is equal to 1.
2.3 if not return out of the method
2.3 if it is - do your processing
2.4 when you have finished processing turn the timer back on

        private Timer timer;

        public void Start()
            timer = new Timer(86400000);
            timer.Elapsed += TimerElapsed;
            timer.Enabled = true;
        private void TimerElapsed(object sender, ElapsedEventArgs e)
                timer.Enabled = false;
                var now = DateTime.Now;

                if (now.Day != 1)

                //else do you processing

               timer.Enabled = true;

Open in new window

btw - Timer is System.Timers.Timer
Umashankar_GunaAuthor Commented:
Thanks for providing the Very good link which provides teh solution. it works perfect.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.