Skip to content

Instantly share code, notes, and snippets.

@TravisTheTechie
Created August 6, 2010 01:11
Show Gist options
  • Select an option

  • Save TravisTheTechie/510671 to your computer and use it in GitHub Desktop.

Select an option

Save TravisTheTechie/510671 to your computer and use it in GitHub Desktop.

Revisions

  1. TravisTheTechie created this gist Aug 6, 2010.
    59 changes: 59 additions & 0 deletions TopshelfStuffExample.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    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();
    }
    }
    }