using System.Diagnostics; using System.ServiceProcess; using System.Timers; using DTS.Common.Service.Properties; // ReSharper disable InconsistentNaming namespace DTS.Common.Service { /// /// Walkthrough: https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx /// Service Application Programming Architecture: https://msdn.microsoft.com/en-us/library/yzk7ksy2(v=vs.100).aspx /// /// public partial class DTSService : ServiceBase { public DTSService() { InitializeComponent(); if (!EventLog.SourceExists(Settings.Default.ServiceName)) { EventLog.CreateEventSource(Settings.Default.ServiceName, Settings.Default.Service); } eventLog.Source = Settings.Default.ServiceName; eventLog.Log = Settings.Default.Service; if (!Settings.Default.Monitoring) return; var timer = new Timer { Interval = Settings.Default.Interval}; // default - 60 seconds timer.Elapsed += OnTimer; timer.Start(); } public void OnTimer(object sender, ElapsedEventArgs args) { // TODO: Insert monitoring activities here. eventLog.WriteEntry("Monitoring the System", EventLogEntryType.Information); } protected override void OnStart(string[] args) { eventLog.WriteEntry(Settings.Default.ServiceName + " started", EventLogEntryType.Information); } protected override void OnStop() { eventLog.WriteEntry(Settings.Default.ServiceName + " stopped", EventLogEntryType.Information); } public enum CustomCommands { Command1 = 128, Command2 = 129, Command3 = 130 } protected override void OnCustomCommand(int command) { base.OnCustomCommand(command); switch (command) { case (int)CustomCommands.Command1: break; case (int)CustomCommands.Command2: break; case (int)CustomCommands.Command3: break; } } } }