public void Shutdown() { lock (monitor) { toShutdown = true; do { try { foreach (var entry in subscribers) { MonitorEx.Pulse(monitor, entry.Value); } if (doneShutdown) { return; //prevent infinite wait due to lost signal } MonitorEx.Wait(monitor, shutdownCondition); } catch (ThreadInterruptedException) { if (subscribers.Count == 0) { Thread.CurrentThread.Interrupt(); break; } throw; } } while (!doneShutdown); } }
public void PublishEvent <E>(E message) where E : class { lock (monitor) { if (toShutdown) { throw new InvalidOperationException(); } List <Subscriber> subs; if (subscribers.TryGetValue(typeof(E), out subs)) { foreach (Subscriber s in subs) { s.TryAddEvent(message); } MonitorEx.Pulse(monitor, subs); } else { throw new InvalidOperationException(); } } }