Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ServiceBase.Run(ServiceBase[] ) method run all services in array asynchronously?

ServiceBase[] servicesToRun;
            servicesToRun = new ServiceBase[]
            {
                new Service1(),
                new Service2()
            };
            ServiceBase.Run(servicesToRun);

How will this work? Will Service1 be run, and when its OnStart() is done, Service2's OnStart() will be run, or both of them will run at the same time? If that's the case, and I explicitly want Service1's OnStart to be done before running Service2.OnStart(), how would I go?

Would this do the trick:

ServiceBase.Run(new Service1())
ServiceBase.Run(new Service2())
like image 757
Mefhisto1 Avatar asked Aug 03 '15 06:08

Mefhisto1


People also ask

What is ServiceBase run?

The ServiceBase. Run method is called in much the same way as the Application. Run method for Windows Forms applications. If AutoLog is true , an entry is written to the event log if any service in the array fails to start.

What is C# ServiceBase?

Run(ServiceBase) Registers the executable for a service with the Service Control Manager (SCM). Run(ServiceBase[]) Registers the executable for multiple services with the Service Control Manager (SCM).


1 Answers

You are misunderstanding what ServiceBase.Run() actually does.

From the documentation:

Registers the executable for multiple services with the Service Control Manager (SCM).

The sequence goes like this:

  • A start request is received for one (or perhaps both) services.
  • The SCM launches the executable.
  • Your main function calls ServiceBase.Run().
  • The SCM calls OnStart() for the service matching the start request, in a new thread. Unless both services are being started, OnStart() will not be called for the other one!

If both services are being started then the order in which OnStart() is called is indeterminate, unless one service has been configured to have a dependency on the other. Configuring such a dependency is the only safe way to ensure a particular ordering, any trickery such as waiting in one OnStart() for a signal from the other one may cause the SCM to deadlock.

If only one service was started, and later on the other service is started, the SCM will call the OnStart() for the second service at that point.

The call to ServiceBase.Run() will not return until the service reports that it is stopped. If you have registered more than one service, it will not return until all the services are stopped. At that point your process is expected to exit.


Note that your second code segment is definitely wrong. The first call won't return until Service1 has been stopped, at which point the second call is illegal and will probably fail - at any rate, it certainly won't do anything useful.

Also note that if the two services cannot run independently of one another, there is probably no point in having two services in the first place. The only case I could think of where it would be sensible is if you want the user to be able to stop and start Service2 while leaving Service1 running.

like image 163
Harry Johnston Avatar answered Sep 19 '22 23:09

Harry Johnston