height8

About


height8 on AppDotNet @height8 on Twitter height8 on Github height8.com on Skype

WANTED

DEC LA36 DECwriter II Terminal

History Of Apple


INFO

Resources

Wi-Fi Security

Updating BeagleBone Black eMMC Angstrom

Where to eat in Toronto

Mono UnixSignal Handler

Turning on named / BIND / DNS server


Mono UnixSignal Handler

www.height8.com/info/devel/languages/csharp/unixsignal

Last update: Jan 16 2011 03:20


If you find that signal handler code isn't firing for you, I've created a larger example which may be helpful:

using System;
using System.Threading;
using Mono.Unix;
using Mono.Unix.Native;

namespace SignalExample
{
    class MainClass
    {
        public static bool masterExit = false;

        public static void Main (string[] args)
        {
            setupHandlers();
            while (!masterExit)
            {
                Thread.Sleep(0);     // other stuff happens
                Thread.Sleep(5000);  // keep Main busy for 5 seconds doing nothing
            }
        }

        private static void setupHandlers()
        {
            Thread newthread = new Thread(new ThreadStart(sigHan));
            newthread.Start();
        }

        private static void sigHan()
        {
            // signal handler thread
            // repeatedly waits for incoming signals until masterExit

            UnixSignal[] signals = new UnixSignal [] {
                new UnixSignal (Signum.SIGINT),
                new UnixSignal (Signum.SIGTERM),
                new UnixSignal (Signum.SIGQUIT),
                new UnixSignal (Signum.SIGHUP),
            	};

            while (!masterExit) {
                int index = UnixSignal.WaitAny (signals,-1);
                Signum signal = signals [index].Signum;
                sigHandler(signal);
            };
        }

        private static void sigHandler(Signum signal)
        {
            switch (signal)
            {
                case Signum.SIGINT:    // Control-C
                    Console.WriteLine("Processing Unix Signal SIGINT");
                    masterExit = true;
                    break;
                case Signum.SIGTERM:
                    Console.WriteLine("Processing Unix Signal SIGTERM");
                    masterExit = true;
                    break;
                case Signum.SIGQUIT:
                    Console.WriteLine("Processing Unix Signal SIGQUIT");
                    masterExit = true;
                    break;
                case Signum.SIGHUP:
                    Console.WriteLine("Processing Unix Signal SIGHUP");
                    Console.WriteLine("Reconfiguring program");
                    break;
            }

            if (masterExit)
            {
                commenceShutdown();
            }
        }

        private static void commenceShutdown()
        {
            masterExit = true;  // we may call commenceShutdown from elsewhere
            Console.WriteLine("Commencing shutdown");
            // stop system timers, close databases, disconnect TCP/IP, etc.
            Console.WriteLine("HALT");
        }

    }
}

 
To provide feedback, corrections or suggestions, please send an email to: back feed @ height8 .com (remove the spaces from the email address if you're doing a copy/paste). In your email, please mention you are referring to: info/devel/languages/csharp/unixsignal