Last active
April 13, 2016 02:09
-
-
Save AlbertoMonteiro/7432b7743f85be48fb8030143578dcfe to your computer and use it in GitHub Desktop.
Post: Criando Windows Services com C# de forma fácil http://blog.albertomonteiro.net.br/2016/04/12/criando-windows-services-com-c-de-forma-facil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class GeradorDeRelatorio : ServiceControl | |
{ | |
private readonly CancellationTokenSource _cancellationTokenSource; | |
public GeradorDeRelatorio() | |
{ | |
_cancellationTokenSource = new CancellationTokenSource(); | |
} | |
public bool Start(HostControl hostControl) | |
{ | |
Task.Run(() => | |
{ | |
while (true) | |
{ | |
//Gera relatorio | |
Thread.Sleep(TimeSpan.FromSeconds(5)); | |
} | |
}, _cancellationTokenSource.Token); | |
return true; | |
} | |
public bool Stop(HostControl hostControl) | |
{ | |
_cancellationTokenSource.Cancel(); | |
_cancellationTokenSource.Dispose(); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Install-Package Topshelf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
HostFactory.Run(configurator => | |
{ | |
configurator.Service<GeradorDeRelatorio>(s => | |
{ | |
s.ConstructUsing(name => new GeradorDeRelatorio()); | |
s.WhenStarted((service, control) => service.Start(control)); | |
s.WhenStopped((service, control) => service.Stop(control)); | |
}); | |
configurator.RunAsLocalSystem(); | |
configurator.SetDescription("Sample Topshelf Host"); | |
configurator.SetDisplayName("Stuff"); | |
configurator.SetServiceName("Stuff"); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
HostFactory.Run(configurator => | |
{ | |
configurator.UseNinject(new MeuNinjectModule(), new MeuNinjectModule2()); | |
configurator.Service<GeradorDeRelatorio>(s => | |
{ | |
s.ConstructUsingNinject(); | |
s.WhenStarted((service, control) => service.Start(control)); | |
s.WhenStopped((service, control) => service.Stop(control)); | |
}); | |
configurator.RunAsLocalSystem(); | |
configurator.SetDescription("Sample Topshelf Host"); | |
configurator.SetDisplayName("Stuff"); | |
configurator.SetServiceName("Stuff"); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment