Skip to content

Instantly share code, notes, and snippets.

@TravisTheTechie
Created August 6, 2010 01:11
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 TravisTheTechie/510671 to your computer and use it in GitHub Desktop.
Save TravisTheTechie/510671 to your computer and use it in GitHub Desktop.
namespace Stuff
{
using System;
using System.IO;
using System.Timers;
using log4net.Config;
using Topshelf;
using Topshelf.Configuration;
using Topshelf.Configuration.Dsl;
internal class Program
{
static void Main(string[] args)
{
XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config"));
RunConfiguration cfg = RunnerConfigurator.New(x =>
{
x.AfterStoppingTheHost(h => { Console.WriteLine("Custome AfterStop called invoked, services are stopping"); });
x.ConfigureService<TownCrier>(s =>
{
s.Named("tc");
s.HowToBuildService(name=> new TownCrier());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("Sample Topshelf Host");
x.SetDisplayName("Stuff");
x.SetServiceName("stuff");
});
Runner.Host(cfg, args);
}
}
public class TownCrier
{
readonly Timer _timer;
public TownCrier()
{
_timer = new Timer(1000) {AutoReset = true};
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine(DateTime.Now);
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment