-2

I know that windows service application in C# provides methods like OnStart() and OnStop(). For example I say that I have written a method in a previous program and i call it in method OnStart() of the service:

MyClass.Start();

This method runs a while loop and exits only on failure.

Problem is, when this method with loop inside is executed in OnStart() method and I start service in Windows, service reports status "Starting", and after some time exits, so the service is never reported that has started.

Otherwise, when my method breaks quickly (for example, some parameters are missing), service reports a "Running" state, but the method has exited.

My question is how can I program Windows service to report "Running" state while my method is running, and stop the service when method exits (exception)?

1 Answers1

0

This is not how it works. The purpose of the Windows service is to run at a specific interval. You need to add Timer control and in OnStart method you enable the control which will trigger Timer_elapsed event. In the event handler you will disable timer first then perform the main work. At the end, you enable the timer.

Vasya
  • 469
  • 2
  • 6