using System; using System.Diagnostics; using System.ServiceProcess; namespace ChadSoft.Edificator.Service { public partial class EdificationService : ServiceBase { private Runner _runner; public EdificationService() { InitializeComponent(); } protected override void OnStart(string[] args) { _runner = new Runner(); _runner.Exception += HandleException; _runner.Start(); EventLog.WriteEntry("Edification Service started.", EventLogEntryType.Information); } protected override void OnStop() { DisposeRunner(); EventLog.WriteEntry("Edification Service stopped.", EventLogEntryType.Information); } protected override void OnShutdown() { DisposeRunner(); EventLog.WriteEntry("Edification Service shut down.", EventLogEntryType.Information); base.OnShutdown(); } private void HandleException(object sender, RunnerExceptionEventArgs e) { Exception exception = e.Exception; string message = string.Format("Exception in Edification Service! Message: {0}\nStack Trace: {1}", exception.Message, exception.StackTrace); EventLog.WriteEntry(message, EventLogEntryType.Error); } private void DisposeRunner() { if (_runner == null) return; _runner.Stop(); _runner.Dispose(); _runner = null; } } }