Start Windows Service automatically

Asked

Viewed 1,247 times

5

I created a Windows Service test, I have been researching and saw that to start it automatically you need to change the property StartType of the object serviceInstaller for Automatic, that the service, once installed, already starts automatically. However, I looked at Computer Management in the Services part and the Boot Type of my service is as Manual. Below prints down below:

SS Service in Computer Management

SS Settings for the Internet service

Code of Projectinstaller.Cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;


namespace servico_teste
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
    public ProjectInstaller()
    {
        InitializeComponent();
    }

    private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
    {
        using (var sc = new ServiceController(serviceInstaller1.ServiceName))
        {
            sc.Start();
        }
    }

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

Service code1.Cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.IO;


namespace servico_teste
{
public partial class Service1 : ServiceBase
{
    Timer timer1;

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer1 = new Timer(new TimerCallback(timer1_Tick), null, 15000, 60000);
    }

    protected override void OnStop()
    {
        StreamWriter vWriter = new StreamWriter(@"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 have the service automatically started after installation?

  • And you can’t change the Service in Computer Management for automatic?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

2

I found a response in the OS.

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();
    }
}

I put in the Github for future reference.

  • It didn’t, I think I’m doing something wrong. The line this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall); on the part of ServiceInstaller_AfterInstall is with the error: The name 'Serviceinstaller_afterinstall' does not exist in the Current context.I changed the name of my object serviceInstaller1_AfterInstall but still the service does not start automatically.

  • It’s meant to be, it’s right below.

  • The problem was the same name. However the service is still with manual boot type.

  • There is some problem in the code, but as you did not post it, there is no help. VS screen does not help anything.

  • I added the codes to the @bigown question

  • These code snippets don’t show anything relevant to what you’re talking about. By the way, the question has already been answered. This seems to be another problem. When asking about it specifically, put the code that configures every installation (class ServiceInstaller).

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.