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:
Service in Computer Management
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?
– Marco Souza
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?
– Maniero