public static void Main(string[] args) { PerfmonService service = new PerfmonService(); ServiceHost selfHost = new ServiceHost(service); try { #if DEBUG ServiceEndpoint endpoint = selfHost.Description.Endpoints[0]; endpoint.EndpointBehaviors.Add(new DebugBehavior()); #endif selfHost.Open(); var timer = new System.Timers.Timer(); timer.Interval = 5000; timer.Elapsed += (s, e) => service.Update(); timer.Start(); Console.WriteLine("Press <Ctrl+C> to terminate service."); ManualResetEvent quitEvent = new ManualResetEvent(false); Console.CancelKeyPress += (s, e) => { quitEvent.Set(); e.Cancel = true; }; quitEvent.WaitOne(); timer.Stop(); Console.WriteLine("Closing connections, please wait..."); selfHost.Close(); } catch (CommunicationException ce) { Console.WriteLine("An exception occurred: {0}", ce.Message); selfHost.Abort(); } }
/* * private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) * { * var ex = (Exception) e.ExceptionObject; * * try * { * File.AppendAllText("error.log", ex.Message + Environment.NewLine); * } * catch (Exception) { } * } */ protected override void OnStart(string[] args) { service = new PerfmonService(); if (serviceHost != null) { serviceHost.Close(); } serviceHost = new ServiceHost(service); serviceHost.Open(); if (timer != null) { timer.Stop(); timer.Dispose(); } timer = new Timer(); timer.Interval = 5000; timer.Elapsed += (s, e) => service.Update(); timer.Start(); }