Service1.cs file code:
Timer timer;
public Service1()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
// get today's date at 00:01 AM
DateTime time = DateTime.Today.AddMinutes(1);
// if 00:01 AM has passed, get tomorrow at 00:01 AM
if (DateTime.Now > time)
time = time.AddDays(1);
// calculate milliseconds until the next 00:01 AM.
int timeToFirstExecution = (int)time.Subtract(DateTime.Now).TotalMilliseconds;
// calculate the number of milliseconds in 24 hours.
int timeBetweenCalls = (int)new TimeSpan(24, 0, 0).TotalMilliseconds;
// set the method to execute when the timer executes.
TimerCallback methodToExecute = Brijeshmethod;
// start the timer. The timer will execute "Brijeshmethod" when the number of seconds between now and
// the next 00:01 AM elapse. After that, it will execute every 24 hours.
timer = new System.Threading.Timer(methodToExecute, null, timeToFirstExecution, timeBetweenCalls);
}
private void Brijeshmethod(object source)
{
//write your code here
}
protected override void OnStop()
{
Thread.Sleep(Timeout.Infinite);
}
Program file:
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
Service1 myService = new Service1();
myService.OnDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif
}
Timer timer;
public Service1()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
// get today's date at 00:01 AM
DateTime time = DateTime.Today.AddMinutes(1);
// if 00:01 AM has passed, get tomorrow at 00:01 AM
if (DateTime.Now > time)
time = time.AddDays(1);
// calculate milliseconds until the next 00:01 AM.
int timeToFirstExecution = (int)time.Subtract(DateTime.Now).TotalMilliseconds;
// calculate the number of milliseconds in 24 hours.
int timeBetweenCalls = (int)new TimeSpan(24, 0, 0).TotalMilliseconds;
// set the method to execute when the timer executes.
TimerCallback methodToExecute = Brijeshmethod;
// start the timer. The timer will execute "Brijeshmethod" when the number of seconds between now and
// the next 00:01 AM elapse. After that, it will execute every 24 hours.
timer = new System.Threading.Timer(methodToExecute, null, timeToFirstExecution, timeBetweenCalls);
}
private void Brijeshmethod(object source)
{
//write your code here
}
protected override void OnStop()
{
Thread.Sleep(Timeout.Infinite);
}
Program file:
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
Service1 myService = new Service1();
myService.OnDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif
}
No comments:
Post a Comment