private static void HookUnixCancelSignals()
        {
            // SetConsoleCtrlHandler not exposed on mono
            // Seems like a bit of a complex topic http://mono.1490590.n4.nabble.com/Control-C-handler-td1532196.html
            // Some hope http://stackoverflow.com/questions/6546509/detect-when-console-application-is-closing-killed
            // http://www.mono-project.com/docs/faq/technical/#operating-system-questions

            // Need to conditionally reference Mono.Posix for all this to work
            // https://github.com/ninjarobot/MonoConditionalReference
            // Catch SIGINT and SIGUSR1
            UnixSignal[] signals = new UnixSignal [] {
                new UnixSignal(Signum.SIGINT),
                new UnixSignal(Signum.SIGUSR1),
            };

            var token         = new ProcessCancellationToken();
            var signal_thread = new Thread(() =>
            {
                while (true)
                {
                    // Wait for a signal to be delivered
                    int index = UnixSignal.WaitAny(signals, -1);

                    var signal = signals[index].Signum;

                    // Notify the main thread that a signal was received
                    CancelRequested = true;
                }
            });
        }
Exemple #2
0
        static int Main(string[] args)
        {
            //var cancelRequested = false;
            //Console.TreatControlCAsInput = true;
            //Console.CancelKeyPress += (o,e) => _cancelRequested = 1;

            // Here's a good post on capturing *all* termination signals
            // http://www.developersalley.com/blog/post/2011/05/13/How-To-Catch-CTRL-C-Or-Break-In-C-Console-Application.aspx
            // http://www.codeproject.com/Articles/2357/Console-Event-Handling
            // Better than Console.TreatControlCAsInput
            // but *oh dear* not cross-platform
            ProcessCancellationToken.SetupHooks();

            var client = new NeoPixels(LED_COUNT);

            using (var session = client.Open())
            {
                for (int i = 0; i < LED_COUNT; i++)
                {
                    if (ProcessCancellationToken.CancelRequested || CancelKeyPressed())
                    {
                        return(0);
                    }

                    Console.WriteLine("Set LED {0}", i);
                    session.SetPixel(i, 0x00FF0000);
                    session.Render();

                    if (ProcessCancellationToken.CancelRequested || CancelKeyPressed())
                    {
                        Thread.Sleep(100);
                    }
                }
            }
            return(0);
        }
        private static void HookUnixCancelSignals()
        {
            // SetConsoleCtrlHandler not exposed on mono
            // Seems like a bit of a complex topic http://mono.1490590.n4.nabble.com/Control-C-handler-td1532196.html
            // Some hope http://stackoverflow.com/questions/6546509/detect-when-console-application-is-closing-killed
            // http://www.mono-project.com/docs/faq/technical/#operating-system-questions

            // Need to conditionally reference Mono.Posix for all this to work
            // https://github.com/ninjarobot/MonoConditionalReference
            // Catch SIGINT and SIGUSR1
            UnixSignal[] signals = new UnixSignal [] {
                new UnixSignal (Signum.SIGINT),
                new UnixSignal (Signum.SIGUSR1),
            };

            var token = new ProcessCancellationToken(); 
            var signal_thread = new Thread(() =>
            {
                while (true)
                {
                    // Wait for a signal to be delivered
                    int index = UnixSignal.WaitAny(signals, -1);

                    var signal = signals[index].Signum;

                    // Notify the main thread that a signal was received
                    CancelRequested = true;
                }
            });
        }