Running a windows service on specific time and a given interval.
The below service is scheduled to run between – 4.50 PM – 5.00 PM
every day. The service will run for the interval of 15 minutes.
|
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.Threading.Tasks;
using
System.Windows.Forms;
namespace EAdvicesServices
{
partial class Service1 : ServiceBase
{
System.Timers.Timer timer1 = null;
public Service1()
{
InitializeComponent();
this.CanHandlePowerEvent
= true;
this.CanHandleSessionChangeEvent
= true;
this.CanPauseAndContinue
= true;
this.CanShutdown = true;
this.CanStop = true;
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
//For each process timer should be mentioned here.
double elapseTime = Convert.ToDouble("900000");
timer1
= new
System.Timers.Timer(elapseTime);
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.Start();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down
necessary to stop your service.
timer1.Stop();
timer1.Enabled = false;
}
protected void timer1_Elapsed(object source,
System.Timers.ElapsedEventArgs aa)
{
TimeSpan currentDate = DateTime.Now.TimeOfDay;
TimeSpan scheduleStartDate
= DateTime.Parse("1/1/2013 04:50:00 PM").TimeOfDay;
TimeSpan scheduleEndDate =
DateTime.Parse("1/1/2013 05:00:00 PM").TimeOfDay;
if ((TimeSpan.Compare(currentDate,
scheduleStartDate) > 0) && (TimeSpan.Compare(currentDate, scheduleEndDate) < 0))
{
RunServiceProgram();
}
}
private void
RunServiceProgram()
{
//Write here you code to execute the program
}
}
}
|
Monday, December 8, 2014
Windows Service to run a function at specified time
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment