public static int Main (string[] args) { using (ServiceHost host = new ServiceHost (typeof (HangmanService))) { var security = new SecurityMode (); host.AddServiceEndpoint ( typeof(IHangmanService), new WSHttpBinding (security, true), "http://localhost:8325/"); host.Open (); /* Console.WriteLine ("Type [CR] to stop ..."); Console.ReadKey (); */ /* Demon */ UnixSignal sigint = new UnixSignal (Signum.SIGINT); UnixSignal sigterm = new UnixSignal (Signum.SIGTERM); UnixSignal sighup = new UnixSignal (Signum.SIGHUP); UnixSignal sigusr2 = new UnixSignal (Signum.SIGUSR2); UnixSignal [] signals = new UnixSignal[] { sigint, sigterm, sighup, sigusr2 }; bool exit = false; while (!exit) { int id = UnixSignal.WaitAny (signals); if (id >= 0 && id < signals.Length) { if (sigint.IsSet || sigterm.IsSet) { sigint.Reset (); sigterm.Reset (); exit = true; } else if (sighup.IsSet) sighup.Reset (); else if (sigusr2.IsSet) sighup.Reset (); } } /* Demon */ host.Close (); } return 0; }
public void TestDispose2 () { UnixSignal a = new UnixSignal (Signum.SIGINT); UnixSignal b = new UnixSignal (Signum.SIGINT); Stdlib.raise (Signum.SIGINT); SleepUntilSignaled (a); Assert.AreEqual (a.Count, 1); Assert.AreEqual (b.Count, 1); b.Close (); a.Reset (); Stdlib.raise (Signum.SIGINT); SleepUntilSignaled (a); Assert.AreEqual (a.Count, 1); a.Close (); }
// The main service loop private void MainLoop (ServiceBase [] services) { try { ServiceBase service; if (services == null || services.Length == 0){ error (logname, "No services were registered by this service"); return; } // Start up the service. service = null; if (name != null){ foreach (ServiceBase svc in services){ if (svc.ServiceName == name){ service = svc; break; } } } else { service = services [0]; } if (service.ExitCode != 0) { //likely due to a previous execution, so we need to reset it to default service.ExitCode = 0; } call (service, "OnStart", args); info (logname, "Service {0} started", service.ServiceName); UnixSignal intr = new UnixSignal (Signum.SIGINT); UnixSignal term = new UnixSignal (Signum.SIGTERM); UnixSignal usr1 = new UnixSignal (Signum.SIGUSR1); UnixSignal usr2 = new UnixSignal (Signum.SIGUSR2); UnixSignal[] sigs = new UnixSignal[]{ intr, term, usr1, usr2 }; for (bool running = true; running; ){ int idx = UnixSignal.WaitAny (sigs); if (idx < 0 || idx >= sigs.Length) continue; if ((intr.IsSet || term.IsSet) && service.CanStop) { intr.Reset (); term.Reset (); info (logname, "Stopping service {0}", service.ServiceName); call (service, "OnStop", null); if (service.ExitCode != 0) error (logname, "Service stopped with a non-zero ExitCode: {0}", service.ExitCode); running = false; } else if (usr1.IsSet && service.CanPauseAndContinue) { usr1.Reset (); info (logname, "Pausing service {0}", service.ServiceName); call (service, "OnPause", null); } else if (usr2.IsSet && service.CanPauseAndContinue) { usr2.Reset (); info (logname, "Continuing service {0}", service.ServiceName); call (service, "OnContinue", null); } } } finally { // Clean up foreach (ServiceBase svc in services){ svc.Dispose (); } } }