Reputation: 18068
When I create a win service I star listening on a specified port. I want to be sure that all the process of listening on that port has finished successfully before the service has running status and Start-Service
command will be finished.
When I invoke Start-Service -Name $NameOfTheService
in Powershell when does powershell decides to go to the next instruction?
Is this code can cause potential issues:
public WinService()
{
InitializeComponent();
OpenPortsCreateEndpoints();
}
Should I put openning ports for instance in protected override void OnStart(string[] args)
or somewhere else?
Upvotes: 1
Views: 51
Reputation: 13227
Powershell doesn't interact with the Service directly, it uses the inbuilt Service Control Manager (SCM).
Powershell requests SCM to start the service, it does so and the service then reports its status to SCM. Powershell itself just waits for SCM to signal that the service status is Running
before continuing.
Services normally report their status as SERVICE_START_PENDING
while they are initialising, then signal SERVICE_RUNNING
once they are in a ready state.
The OnStart
method must return to the operating system after the service's operation has begun. It must not loop forever or block, so use it only to initialise the service.
Walkthrough: Create a Windows service app covers all the basics, and contains example code for Set service status.
Upvotes: 1