This is a Windows Service project using VS2015 and .NET Framework 4.5.
I'm trying to install my service through a post-build action and then start it automatically by using ServiceController.Start()
. Here's my code that tries to start the service:
private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
using (var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()))
{
using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
{
try
{
sw.Write("Starting service...");
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
sw.Write("Service status is now set to {0}.", sc.Status.ToString());
}
catch (InvalidOperationException)
{
sw.Write("Could not start the service.");
}
}
}
}
The service installs just fine, but my ServiceController.WaitForStatus()
call seemingly keeps waiting forever. I have tried calling it from Committed
and AfterInstall
events.