Search code examples
c#serviceinstallation

Automatically starting a service when the installer finishes


Possible Duplicate:
How to automatically start your service after install?

I have a Visual Studio 2008 C# .NET 3.5 service installer project (MSI) running on Windows 7 x64.

I subscribe to the ServiceInstaller.OnAfterInstall notification to start my service when the installation finishes.

[RunInstaller(true)]
public partial class MyInstaller : Installer
{
    private System.ServiceProcess.ServiceInstaller my_installer_;

    private void InitializeComponent()
    {
        // ...
        this.my_installer_.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.OnAfterInstall);
        // ...
    }

    private void OnAfterInstall(object sender, InstallEventArgs e)
    {
        using (System.ServiceProcess.ServiceController svc =
            new System.ServiceProcess.ServiceController("MyService"))
        {
            svc.Start(); // completes successfully
        }
    }
}

Though the function succeeds without exception, my service is never running when the installer finishes.

The event log shows no failures related to service startup and if I go to the services manager, I can start the service manually (or restart the PC and it will start automatically).

What do I need to do to automatically start my service when the installer process finishes?


Solution

  • I assume that Start returns immediatly, and Starts the Service in the background. Check the Docs: http://msdn.microsoft.com/en-us/library/yb9w7ytd.aspx