Exemplo n.º 1
0
        /// <summary>
        /// 生成异步拦截器,即before、targetFn、after、error会按顺序执行但仅before函数阻塞当前线程,后续函数均在另一个线程中执行。
        /// </summary>
        /// <param name="targetFn">原函数</param>
        /// <param name="before">前置拦截器</param>
        /// <param name="after">后置拦截器</param>
        /// <param name="error">异常处理</param>
        /// <param name="must">无论before、targeFn、after和error函数执行结果如何都必须执行的函数</param>
        /// <returns>加工后带拦截器的函数</returns>
        public static FnWithInterceptor CreateAsyncInterceptor(TargetFn targetFn, Before before = null, After after = null, Error error = null, Must must = null)
        {
            return(() =>
            {
                PipingItemReturnValue returnValue = new PipingItemReturnValue();
                if (null != before)
                {
                    returnValue = before();
                }

                if (null != targetFn)
                {
                    ThreadPool.QueueUserWorkItem((state) =>
                    {
                        try
                        {
                            returnValue = targetFn(returnValue);
                            if (null != after)
                            {
                                after(returnValue);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (null != error)
                            {
                                returnValue.Ex = ex;
                                error(returnValue);
                            }
                        }
                        finally
                        {
                            if (null != must)
                            {
                                must(returnValue);
                            }
                        }
                    });
                }
                else if (null != must)
                {
                    must(returnValue);
                }
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// 管道流,当管道流中的其中一个模块返回false时则跳出管道并执行回调函数
        /// </summary>
        /// <param name="pipingItems">管道</param>
        /// <param name="state">管道流域参数</param>
        /// <param name="pipingCallback">跳出管道时的回调函数</param>
        public static void Piping(PipingItem[] pipingItems, IDictionary <string, object> state = null, PipingCallback pipingCallback = null, Interrupter interrupter = null)
        {
            PipingItemReturnValue curPipingItemReturnValue = null;

            if (state == null)
            {
                state = new Dictionary <string, object>();
            }
            if (interrupter != null)
            {
                state["Interrupt"] = interrupter.Interrupt;
            }
            for (int i = 0, len = pipingItems.Length; i < len && (curPipingItemReturnValue == null || curPipingItemReturnValue.IsSuccess); i++)
            {
                curPipingItemReturnValue = pipingItems[i](state, (curPipingItemReturnValue == null ? null : curPipingItemReturnValue.Objs));
            }

            if (pipingCallback == null)
            {
                return;
            }

            pipingCallback(curPipingItemReturnValue);
        }