// 
// (c) 2010 eedv holger@eiboeck.de
// 
using System;
using System.Timers;

namespace KaltgasTool
{
    public class TimerLock : IDisposable
    {
        private readonly Timer _timer;
        
        public static object LockObject { get; private set; }

        static TimerLock()
        {
            LockObject = new Object();
        }

        public TimerLock(Timer timer)
        {
            _timer = timer;
            System.Threading.Monitor.Enter(LockObject);
            Pause(); 
        }

        private void Pause()
        {
            if (_timer == null)
                throw new ArgumentNullException("Timer cannot be null!");

            _timer.Enabled = false;
            _timer.Stop();
            _timer.Close();   
        }

        public void Dispose()
        {
            if (_timer != null)
                _timer.Enabled = true;

            System.Threading.Monitor.Exit(LockObject);
        }
    }
}
