Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active April 13, 2016 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertoMonteiro/7432b7743f85be48fb8030143578dcfe to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/7432b7743f85be48fb8030143578dcfe to your computer and use it in GitHub Desktop.
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;
}
}
Install-Package Topshelf
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");
});
}
}
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