Windows service onStart event

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
 
E

Eliyahu Goldin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top