private void InvokeEventHandler(object eventObject) { List <Task> taskList = null; // ReSharper disable once ForCanBeConvertedToForeach for (int i = 0; i < _eventHandlerList.Count; i++) { // ReSharper disable once InconsistentlySynchronizedField EventHandlerHolder record = _eventHandlerList[i]; if (eventObject == null || record.ParameterType.IsInstanceOfType(eventObject)) { Task task = Task.Run(() => record.MethodInfo.Invoke(record.Handler, new[] { eventObject })); if (taskList == null) { taskList = new List <Task>(); } taskList.Add(task); //record.MethodInfo.Invoke(record.Handler, new[] { eventObject }); } } if (taskList != null) { Task.WaitAll(taskList.ToArray()); } }
private void DispatchMessage(object eventObject) { try { // ReSharper disable once ForCanBeConvertedToForeach for (int i = 0; i < _eventHandlerList.Count; i++) { // ReSharper disable once InconsistentlySynchronizedField EventHandlerHolder record = _eventHandlerList[i]; if (eventObject == null || record.ParameterType.IsInstanceOfType(eventObject)) { Task.Run(() => { try { record.MethodInfo.Invoke(record.Handler, new[] { eventObject }); } catch (Exception ie) { Trace.TraceWarning("Event handler (class '{0}@{1}', method '{2}') failed: {3}{4}{5}{4}eventObject: {6}", record.Handler.GetType(), record.Handler.GetHashCode(), record.MethodInfo, ie.Message, Environment.NewLine, ie.StackTrace, eventObject); } }); } } } catch (Exception de) { Trace.TraceError("Dispatch event ({0}) failed: {1}{2}{3}", eventObject, de.Message, Environment.NewLine, de.StackTrace); } finally { Interlocked.Decrement(ref _pendingEventNumber); } }