public static List<Object> CallDelegatorBySync(Delegate dl, Object[] parameters, bool controlInvoke, bool parallelInvocation) { List<Object> result = new List<Object>(); if (dl != null) { Delegate[] list = dl.GetInvocationList(); if (parallelInvocation) { // parallel execution using (AsyncInvocationContainer aic = new AsyncInvocationContainer()) { result.AddRange(aic.Execute(list, parameters, controlInvoke)); } } else { // sequential execution foreach (Delegate d in list) { try { if (LOGGER.IsDebugEnabled) LOGGER.Debug(string.Format("RAISER, before invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: {3}", d.Target == null ? d.Method.DeclaringType.FullName : d.Target.GetType().FullName, d.Method.Name, controlInvoke.ToString(), parallelInvocation.ToString())); if (controlInvoke && d.Target is Control) { result.Add(((Control)d.Target).Invoke(d, parameters)); } else if (controlInvoke && d.Target is DependencyObject) { DependencyObject ctrl = (DependencyObject)d.Target; result.Add(ctrl.Dispatcher.Invoke(d, parameters)); } else { result.Add(d.Method.Invoke(d.Target, parameters)); } } catch (Exception e) { if (LOGGER.IsErrorEnabled) LOGGER.Error(string.Format("RAISER, failed to invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: {3}. Reason: {4}", d.Target == null ? d.Method.DeclaringType.FullName : d.Target.GetType().FullName, d.Method.Name, controlInvoke.ToString(), parallelInvocation.ToString(), e.Message), e); result.Add(e); } finally { if (LOGGER.IsDebugEnabled) LOGGER.Debug(string.Format("RAISER, after invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: {3}", d.Target == null ? d.Method.DeclaringType.FullName : d.Target.GetType().FullName, d.Method.Name, controlInvoke.ToString(), parallelInvocation.ToString())); } } } } return result; }
public static List <Object> CallDelegatorBySync(Delegate dl, Object[] parameters, bool controlInvoke, bool parallelInvocation) { List <Object> result = new List <Object>(); if (dl != null) { Delegate[] list = dl.GetInvocationList(); if (parallelInvocation) { // parallel execution using (AsyncInvocationContainer aic = new AsyncInvocationContainer()) { result.AddRange(aic.Execute(list, parameters, controlInvoke)); } } else { // sequential execution foreach (Delegate d in list) { try { if (LOGGER.IsDebugEnabled) { LOGGER.Debug(string.Format("RAISER, before invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: {3}", d.Target == null ? d.Method.DeclaringType.FullName : d.Target.GetType().FullName, d.Method.Name, controlInvoke.ToString(), parallelInvocation.ToString())); } if (controlInvoke && UIReflectionHelper.IsObjectWinFormsControl(d.Target) /*d.Target is Control*/) { //result.Add(((Control)d.Target).Invoke(d, parameters)); result.Add(UIReflectionHelper.InvokeOnWinFormsControl(d.Target, d, parameters)); } else if (controlInvoke && UIReflectionHelper.IsObjectWPFDependency(d.Target) /*d.Target is DependencyObject*/) { //DependencyObject ctrl = (DependencyObject)d.Target; //result.Add(ctrl.Dispatcher.Invoke(d, parameters)); result.Add(UIReflectionHelper.InvokeOnWPFDependency(d.Target, d, parameters)); } else { result.Add(d.Method.Invoke(d.Target, parameters)); } } catch (Exception e) { if (LOGGER.IsErrorEnabled) { LOGGER.Error(string.Format("RAISER, failed to invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: {3}. Reason: {4}", d.Target == null ? d.Method.DeclaringType.FullName : d.Target.GetType().FullName, d.Method.Name, controlInvoke.ToString(), parallelInvocation.ToString(), e.Message), e); } result.Add(e); } finally { if (LOGGER.IsDebugEnabled) { LOGGER.Debug(string.Format("RAISER, after invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: {3}", d.Target == null ? d.Method.DeclaringType.FullName : d.Target.GetType().FullName, d.Method.Name, controlInvoke.ToString(), parallelInvocation.ToString())); } } } } } return(result); }