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 List <Object> GetEvents(Subscriber subscriber, Type type) { lock (monitor) { List <Object> eventsToProcess; do { //não existem eventos e foi chamado o shutDown if (subscriber.events.Count == 0 && toShutdown) { return(null); } //foi chamado shutDown mas existem eventos a processar if (toShutdown && subscriber.events.Count != 0) { return(subscriber.RemoveSubscriberEvents()); } try { List <Subscriber> list; subscribers.TryGetValue(type, out list); MonitorEx.Wait(monitor, list); if (subscriber.events.Count != 0) { return(subscriber.RemoveSubscriberEvents()); } } catch (ThreadInterruptedException) { //thread interrompida mas existem eventos a processar if (subscriber.events.Count != 0) { eventsToProcess = subscriber.RemoveSubscriberEvents(); //cancelar na posse do lock Thread.CurrentThread.Interrupt(); break; } throw; } } while (true); return(eventsToProcess); } }