Ejemplo n.º 1
0
            private void InvokerThreadMain(Object param)
            {
                Object[] data  = (Object[])param;
                Delegate dl    = (Delegate)data[1];
                Int32    index = (Int32)data[0];

                try
                {
                    if (LOGGER.IsDebugEnabled)
                    {
                        LOGGER.Debug(string.Format("RAISER, before invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: true", dl.Target == null ? dl.Method.DeclaringType.FullName : dl.Target.GetType().FullName, dl.Method.Name, mControlInvoke.ToString()));
                    }

                    if (mControlInvoke && UIReflectionHelper.IsObjectWinFormsControl(dl.Target))
                    {
                        mResultObjects[index] = UIReflectionHelper.InvokeOnWinFormsControl(dl.Target, dl, mParameters); //((Control)dl.Target).Invoke(dl, mParameters);
                    }
                    else if (mControlInvoke && UIReflectionHelper.IsObjectWPFDependency(dl.Target))
                    {
                        //DependencyObject ctrl = (DependencyObject)dl.Target;
                        //mResultObjects[index] = ctrl.Dispatcher.Invoke(dl, mParameters);
                        mResultObjects[index] = UIReflectionHelper.InvokeOnWPFDependency(dl.Target, dl, mParameters);
                    }
                    else
                    {
                        mResultObjects[index] = dl.Method.Invoke(dl.Target, mParameters);
                    }
                }
                catch (Exception e)
                {
                    if (LOGGER.IsErrorEnabled)
                    {
                        LOGGER.Error(string.Format("RAISER, failed to invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: true. Reason: {3}", dl.Target == null ? dl.Method.DeclaringType.FullName : dl.Target.GetType().FullName, dl.Method.Name, mControlInvoke.ToString(), e.Message), e);
                    }
                    mResultObjects[index] = e;
                }
                finally
                {
                    if (LOGGER.IsDebugEnabled)
                    {
                        LOGGER.Debug(string.Format("RAISER, after invoke on '{0}' with method '{1}'. Invoke on control: {2}, parallel: true", dl.Target == null ? dl.Method.DeclaringType.FullName : dl.Target.GetType().FullName, dl.Method.Name, mControlInvoke.ToString()));
                    }
                }
                mExecutorThreadSemaphore.Release();
            }
Ejemplo n.º 2
0
        private void ExecuteTask(object state)
        {
            QueueItem item         = state as QueueItem;
            Exception exception    = null;
            object    methodResult = null;

            try
            {
                if (InvokeUIAtAction && UIReflectionHelper.IsObjectWinFormsControl(item.TaskDelegate.Target))
                {
                    methodResult = UIReflectionHelper.InvokeOnWinFormsControl(item.TaskDelegate.Target, item.TaskDelegate, item.InParameters); //((Control)item.TaskDelegate.Target).Invoke(item.TaskDelegate, item.InParameters);
                }
                else if (InvokeUIAtAction && UIReflectionHelper.IsObjectWPFDependency(item.TaskDelegate.Target))
                {
                    //DependencyObject ctrl = (DependencyObject)item.TaskDelegate.Target;
                    //methodResult = ctrl.Dispatcher.Invoke(item.TaskDelegate, item.InParameters);
                    methodResult = UIReflectionHelper.InvokeOnWPFDependency(item.TaskDelegate.Target, item.TaskDelegate, item.InParameters);
                }
                else
                {
                    methodResult = item.TaskDelegate.Method.Invoke(item.TaskDelegate.Target, item.InParameters);
                }
            }
            catch (TargetInvocationException ex)
            {
                exception = ex.InnerException;
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            TaskResult result = null;

            if (item.TaskDelegate.Method.ReturnType.Equals(typeof(void)))
            {
                result = new TaskResult(item.InParameters, exception);
            }
            else if (exception == null)
            {
                result = (TaskResult)typeof(TaskResult <>).MakeGenericType(item.TaskDelegate.Method.ReturnType).GetConstructor(new Type[] { typeof(object[]), typeof(Exception), item.TaskDelegate.Method.ReturnType }).Invoke(new object[] { item.InParameters, exception, methodResult });
            }
            else
            {
                result = (TaskResult)typeof(TaskResult <>).MakeGenericType(item.TaskDelegate.Method.ReturnType).GetConstructor(new Type[] { typeof(object[]), typeof(Exception) }).Invoke(new object[] { item.InParameters, exception });
            }

            if (item.ReturnDelegate != null)
            {
                try
                {
                    if (InvokeUIAtReturn && UIReflectionHelper.IsObjectWinFormsControl(item.ReturnDelegate.Target))
                    {
                        //((Control)item.ReturnDelegate.Target).Invoke(item.ReturnDelegate, new object[] { result });
                        UIReflectionHelper.InvokeOnWinFormsControl(item.ReturnDelegate.Target, item.ReturnDelegate, new object[] { result });
                    }
                    else if (InvokeUIAtReturn && UIReflectionHelper.IsObjectWPFDependency(item.ReturnDelegate.Target))
                    {
                        //DependencyObject ctrl = (DependencyObject)item.ReturnDelegate.Target;
                        //ctrl.Dispatcher.Invoke(item.ReturnDelegate, new object[] { result });
                        UIReflectionHelper.InvokeOnWPFDependency(item.ReturnDelegate.Target, item.ReturnDelegate, new object[] { result });
                    }
                    else
                    {
                        item.ReturnDelegate.Method.Invoke(item.ReturnDelegate.Target, new object[] { result });
                    }
                }
                catch (Exception e)
                {
                    if (LOGGER.IsErrorEnabled)
                    {
                        LOGGER.Error(e.Message, e);
                    }
                }
            }

            if (ChaosTheoryMode == ChaosTheoryEnum.Sequential)
            {
                lock (mSequentialQueuedItems)
                {
                    mSequentialQueuedItems.RemoveAt(0);
                    if (mSequentialQueuedItems.Count > 0)
                    {
                        mThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ExecuteTask), mSequentialQueuedItems[0]);
                    }
                    else
                    {
                        mIsExecuting = false;
                    }
                }
            }
            else if (ChaosTheoryMode != ChaosTheoryEnum.Chaos)
            {
                lock (mOrderedItems)
                {
                    int hashCode          = ChaosTheoryMode == ChaosTheoryEnum.OrderByTaskDelegateTarget ? item.TaskDelegate.Target.GetHashCode() : item.ReturnDelegate.Target.GetHashCode();
                    List <QueueItem> list = mOrderedItems[hashCode];
                    list.RemoveAt(0);
                    if (list.Count > 0)
                    {
                        mThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ExecuteTask), list[0]);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        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);
        }