Search code examples
c#multithreadingmonoglibgtk#

Stop/Restart GLib.Timeout.Add();


This function starts a timer in GTK#. I want to be able to start and stop this as I please.

void StartClock ()
 {
    GLib.Timeout.Add (1000, new GLib.TimeoutHandler (AskPython));
 }

Solution

  • Glib timeout doesn't support that but here is a timer class I wrote that mimics Microsoft's timer class.

    public delegate void TimerElapsedHandler (object sender, TimerElapsedEventArgs args);
    
    public class TimerElapsedEventArgs : EventArgs
    {
        DateTime signalTime;
    
        public TimerElapsedEventArgs () {
            signalTime = DateTime.Now;
        }
    }
    
    public class Timer
    {
        private bool _enabled;
        public bool enabled {
            get {
                return _enabled;
            }
            set {
                _enabled = value;
                if (_enabled)
                    Start ();
                else
                    Stop ();
            }
        }
        protected uint timerId;
    
        public event TimerElapsedHandler TimerElapsedEvent;
        public uint timerInterval; 
        public bool autoReset;
    
        public Timer () : this (0) { }
    
        public Timer (uint timerInterval) {
            _enabled = false;
            this.timerInterval = timerInterval;
            autoReset = true;
            timerId = 0;
        }
    
        public void Start () {
            _enabled = true;
            timerId = GLib.Timeout.Add (timerInterval, OnTimeout);
        }
    
        public void Stop () {
            _enabled = false;
            GLib.Source.Remove (timerId);
        }
    
        protected bool OnTimeout () {
            if (_enabled) {
                if (TimerElapsedEvent != null)
                    TimerElapsedEvent (this, new TimerElapsedEventArgs ());
            }
            return _enabled & autoReset;
        }
    }
    

    Usage:

    Timer t = new Timer (1000);
    t.TimerElapsedEvent += (sender, args) => {
        Console.WriteLine (args.signalTime.ToString ());
    };
    t.enabled = true;