示例#1
0
        /// <summary>
        /// Finds applicable subscribers, and has them handle the specified event, on the current thread.
        /// </summary>
        /// <param name="evnt">The event to handle.</param>
        /// <param name="exceptions">Stores exceptions throws by event handlers.</param>
        protected internal void Handle(EventBase evnt, List <Exception> exceptions)
        {
            if (evnt.NullReference())
            {
                throw Exc.Null(nameof(evnt));
            }

            if (exceptions.NullReference())
            {
                throw Exc.Null(nameof(exceptions));
            }

            lock (this.syncLock)
            {
                var eventTypeInfo = evnt.GetType().GetTypeInfo();
                for (int i = 0; i < this.subscribers.Count;)
                {
                    var wrapper = this.subscribers[i];
                    if (EventBase.CanHandle(wrapper.EventHandlerEventTypeInfo, eventTypeInfo))
                    {
                        bool referenceFound = true;
                        try
                        {
                            wrapper.Handle(evnt, out referenceFound);
                        }
                        catch (Exception ex)
                        {
                            exceptions.Add(ex);
                        }

                        if (referenceFound)
                        {
                            // event handled: continue
                            ++i;
                        }
                        else
                        {
                            // GC already collected the event handler: remove wrapper and continue
                            this.subscribers.RemoveAt(i);
                        }
                    }
                    else
                    {
                        // event handler not compatible with event: continue
                        ++i;
                    }
                }
            }
        }