Launch Windows Service automatically

4

I created% test%, I was searching and I saw that to start it automatically it is necessary to change the Windows Service property of the% object StartType to Automatic , that the service, after already started automatically. However, I looked in Computer Management in the Services part and the Startup Type of my service is as Manual. Here are prints below:

ComputerManagementService

ServiceInstallerSettings

ProjectInstaller.csCode

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Configuration.Install;usingSystem.Linq;usingSystem.ServiceProcess;namespaceservico_teste{[RunInstaller(true)]publicpartialclassProjectInstaller:System.Configuration.Install.Installer{publicProjectInstaller(){InitializeComponent();}privatevoidserviceInstaller1_AfterInstall(objectsender,InstallEventArgse){using(varsc=newServiceController(serviceInstaller1.ServiceName)){sc.Start();}}publicvoidServiceInstaller(){//...Installercodeherethis.AfterInstall+=newInstallEventHandler(serviceInstaller1_AfterInstall);}}}

Service1.csCode

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.ServiceProcess;usingSystem.Text;usingSystem.Threading;usingSystem.IO;namespaceservico_teste{publicpartialclassService1:ServiceBase{Timertimer1;publicService1(){InitializeComponent();}protectedoverridevoidOnStart(string[]args){timer1=newTimer(newTimerCallback(timer1_Tick),null,15000,60000);}protectedoverridevoidOnStop(){StreamWritervWriter=newStreamWriter(@"c:\testeServico.txt", true); 
        vWriter.WriteLine("Servico Parado: " + DateTime.Now.ToString()); 
        vWriter.Flush(); 
        vWriter.Close();

    }

    private void timer1_Tick(object sender) 
    { 
        StreamWriter vWriter = new StreamWriter(@"c:\testeServico.txt", true);
        vWriter.WriteLine("Servico Rodando: " + DateTime.Now.ToString()); 
        vWriter.Flush(); 
        vWriter.Close(); 
    }
}
}

The question is: how do I get the service started automatically after the service is installed?

    
asked by anonymous 15.12.2015 / 13:32

1 answer

2

I found a answer in the SO .

public ServiceInstaller() {
    //... Installer code here
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) {
    using (var sc = new ServiceController(serviceInstaller.ServiceName)) {
         sc.Start();
    }
}
    
15.12.2015 / 13:36