view Implab/Parallels/Signal.cs @ 210:5dc21f6a3222 v2

Code review for RunnableComponent Added StaApartment class based on System.Windows.Forms.Application message loop
author cin
date Mon, 20 Mar 2017 17:44:18 +0300
parents 471f596b2603
children
line wrap: on
line source

using System;
using System.Threading;

namespace Implab.Parallels {
    /// <summary>
    /// Implements a simple signalling logic using <see cref="Monitor.PulseAll(object)"/>.
    /// </summary>
    public class Signal {
        readonly object m_lock = new object();
        bool m_state;

        public void Set() {
            lock(m_lock) {
                m_state = true;
                Monitor.PulseAll(m_lock);
            }
        }

        public void Wait() {
            lock (m_lock)
                if (!m_state)
                    Monitor.Wait(m_lock);
        }

        public bool Wait(int timeout) {
            lock (m_lock)
                return m_state || Monitor.Wait(m_lock, timeout);
        }
    }
}