Windows Service OnStart Event  
Author Message
Sarguna





PostPosted: Visual C# General, Windows Service OnStart Event Top

Hello Everybody

I wanted to know how to work with Windows services

In the Service it is said that we should not write looping code in the OnStart Event

So where should i write my code of looping event

as such i wrote in the start event it works fine

Is it good to write the code in onstart as my code will take a time may be more than 30min to 1 hr



And one more thing how to stop the service i.e after my code has done the work

Thank you in advance




Visual C#16  
 
 
Dan Dittenhafer





PostPosted: Visual C# General, Windows Service OnStart Event Top

Sarguna,

The OnStart event handler is not intended to contain the service's 'work' code. Instead try moving your looping code to a function that follows the WaitCallback delegate definition and call ThreadPool.QueueUserWorkItem in the OnStart event. This will allow the OnStart event handler to return quickly, thereby allowing your service is start quickly and respond to the Service Control in a timely manner.

Dan


 
 
Dan Dittenhafer





PostPosted: Visual C# General, Windows Service OnStart Event Top

The ServiceController class in the System.ServiceProcess namespace provides functionality to Start/Stop services. Try calling ServiceControler.Stop as in:

ServiceController cs = new ServiceController("MyService");

cs.Stop();


 
 
RizwanSharp





PostPosted: Visual C# General, Windows Service OnStart Event Top

Cross Post, Has been answered in C# language Forum :)

Best Regards,

Rizwan



 
 
Sarguna





PostPosted: Visual C# General, Windows Service OnStart Event Top

Thank you for the reply