public void TestNestedInvocation() { UnixSignal s = new UnixSignal(Signum.SIGINT); Thread a = new Thread(delegate() { bool r = s.WaitOne (1000, false); }); Thread b = new Thread(delegate() { bool r = s.WaitOne (500, false); }); a.Start(); b.Start(); a.Join(); b.Join(); }
// helper method to create a thread waiting on a UnixSignal static Thread CreateWaitSignalThread (UnixSignal signal, int timeout) { Thread t1 = new Thread(delegate() { DateTime start = DateTime.Now; bool r = signal.WaitOne (timeout, false); DateTime end = DateTime.Now; Assert.AreEqual (signal.Count, 1); Assert.AreEqual (r, true); if ((end - start) > new TimeSpan (0, 0, timeout/1000)) throw new InvalidOperationException ("Signal slept too long"); }); return t1; }
public void TestNoEmit () { using (UnixSignal u = new UnixSignal (Signum.SIGINT)) { DateTime start = DateTime.Now; bool r = u.WaitOne (5100, false); Assert.AreEqual (r, false); DateTime end = DateTime.Now; if ((end - start) < new TimeSpan (0, 0, 5)) throw new InvalidOperationException ("Signal didn't block for 5s; blocked for " + (end-start).ToString()); } }
public void TestRaise () { Thread t1 = new Thread (delegate () { using (UnixSignal a = new UnixSignal (Signum.SIGINT)) { DateTime start = DateTime.Now; bool r = a.WaitOne (5000, false); DateTime end = DateTime.Now; Assert.AreEqual (a.Count, 1); Assert.AreEqual (r, true); if ((end - start) > new TimeSpan (0, 0, 5)) throw new InvalidOperationException ("Signal slept too long"); } }); Thread t2 = new Thread (delegate () { Thread.Sleep (1000); Stdlib.raise (Signum.SIGINT); }); t1.Start (); t2.Start (); t1.Join (); t2.Join (); }
internal static void SetControlCHandler() { if (Utilities.IsWindows) { if (!_windowsConsoleHandlerSet) Console.CancelKeyPress += ConsoleControlCHandler; _windowsConsoleHandlerSet = true; } else if (_signalThread == null) { Stdlib.SetSignalAction(Signum.SIGINT, SignalAction.Default); _signalThread = new Thread(() => { try { using (var sig = new UnixSignal(Signum.SIGINT)) { while (true) { sig.WaitOne(); ControlCHandler(); } } } catch (ThreadAbortException) { } }); _signalThread.Start(); } }
static void Main() { Program.Games = new Dictionary<int, Game>(); dump_fname = Environment.GetEnvironmentVariable("OPENSHIFT_DATA_DIR")+"games.dump"; log_fname = Environment.GetEnvironmentVariable("OPENSHIFT_DIY_LOG_DIR") + "errors.log"; string data_dir = Environment.GetEnvironmentVariable("OPENSHIFT_DATA_DIR"); if (data_dir != null) { System.IO.TextReader file = new System.IO.StreamReader(data_dir+"secret_token.txt", true); SECRET = file.ReadToEnd(); file.Close(); } if (System.IO.File.Exists(dump_fname)) { System.IO.TextReader file = new System.IO.StreamReader(dump_fname, true); string[] f = file.ReadToEnd().Split(new string[]{"\n"},StringSplitOptions.RemoveEmptyEntries); foreach (string el in f) { string[] s = el.Split('@'); Program.Games.Add(int.Parse(s[0]),JsonConvert.DeserializeObject<Game>(s[1])); } file.Close(); } try { WebSocket.Start(); } catch (Exception e) { Log("Startup fatal error: "+e.ToString()); System.Environment.Exit(0); } Log("Start"); UnixSignal term = new UnixSignal(Signum.SIGTERM); term.WaitOne(); Log("Terminate"); SaveGame(); System.Environment.Exit(0); }