Skip to content

Instantly share code, notes, and snippets.

@alex-groshev
Created May 11, 2015 17:08
Show Gist options
  • Select an option

  • Save alex-groshev/fbb61d65fd201a2ec8f7 to your computer and use it in GitHub Desktop.

Select an option

Save alex-groshev/fbb61d65fd201a2ec8f7 to your computer and use it in GitHub Desktop.

Revisions

  1. alex-groshev created this gist May 11, 2015.
    75 changes: 75 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    using System;
    using System.Collections.Generic;
    using System.Timers;
    using Topshelf;

    namespace ConsoleApplication
    {
    public interface IProcess
    {
    void ProcessHandler(object o, ElapsedEventArgs args);
    }

    public class Process1 : IProcess
    {
    public void ProcessHandler(object o, ElapsedEventArgs args)
    {
    Console.WriteLine("Process1");
    }
    }

    public class Process2 : IProcess
    {
    public void ProcessHandler(object o, ElapsedEventArgs args)
    {
    Console.WriteLine("Process2");
    }
    }

    public class ProcessRunner
    {
    public ProcessRunner(IEnumerable<IProcess> processes)
    {
    _timer = new Timer(100);
    foreach (var process in processes)
    {
    _timer.Elapsed += process.ProcessHandler;
    }
    }

    public void Start()
    {
    _timer.Start();
    }

    public void Stop()
    {
    _timer.Stop();
    }

    private readonly Timer _timer;
    }

    class Program
    {
    static void Main(string[] args)
    {
    HostFactory.Run(x =>
    {
    x.Service<ProcessRunner>(s =>
    {
    s.ConstructUsing(name => new ProcessRunner(
    new List<IProcess>
    {
    new Process1(),
    new Process2()
    }));
    s.WhenStarted(_ => _.Start());
    s.WhenStopped(_ => _.Stop());
    });
    x.RunAsLocalSystem();
    // other settings here...
    });
    }
    }
    }