Windows service onStart event

  • Thread starter Thread starter Nishen
  • Start date Start date
N

Nishen

Hi,
I want my service to sit in an endless loop and run a
stored procedure every say, 1 hour.

protected override void OnStart(string[] args)
{

while(1=1)
{
open connection
run sp
close connection

}
The problem is that the service cannot be started and
bombs out. What am doing wrong here?

tia
Nishen
 
All you need to do is just to use a timer. Note, there are 3 timer classes
in .NET. You need one from namespace System.Timers.

private System.Timers.Timer timer;

protected override void OnStart(string[] args)
{
setup timer
start timer
}

Elapsed Event Handler for timer (object sender, ElapsedEventArgs e)
{
open connection
run sp
close connection
restart timer (if was not setup to AutoReset)
}

HTH,

Eliyahu
 
Back
Top