/// <summary> /// Launches a background thread to listen for POSIX signals. /// Exit on two signal 2's or one signal 15. /// </summary> static void InstallSignalHandler(RCRunner runner) { #if __MonoCS__ UnixSignal[] signals = { new UnixSignal(Mono.Unix.Native.Signum.SIGTERM), new UnixSignal(Mono.Unix.Native.Signum.SIGINT) }; Thread signalThread = new Thread(delegate() { while (true) { int index = UnixSignal.WaitAny(signals, -1); if (index < 0) { continue; } Mono.Unix.Native.Signum signal = signals[index].Signum; if (signal == Mono.Unix.Native.Signum.SIGTERM) { ThreadPool.QueueUserWorkItem(delegate(object state) { RCSystem.Log.Record(0, 0, "runner", 0, "signal", "SIGTERM"); runner.Abort(15); }); } else if (signal == Mono.Unix.Native.Signum.SIGINT) { if (!_firstSigint) { _firstSigint = true; ThreadPool.QueueUserWorkItem(delegate(object state) { RCSystem.Log.Record(0, 0, "runner", 0, "signal", "SIGINT"); runner.Interupt(); }); } else { ThreadPool.QueueUserWorkItem(delegate(object state) { RCSystem.Log.Record(0, 0, "runner", 0, "signal", "SIGINT (exiting)"); runner.Abort(2); }); } } } }); signalThread.IsBackground = true; signalThread.Start(); #endif }