public HandlerNode() { previous = next = null; nodeHandler = null; nextTimeOuts = 1000; //默认间隔一秒 handlerName = "undefined"; }
public HandlerNode(HandlerNode previous, AccessHandler cur,string handlerName, int timeOuts = 1000) { this.previous = previous; this.nodeHandler = cur; nextTimeOuts = timeOuts; this.handlerName = handlerName; // 与前一个关联 this.previous.next = this; }
private void HandlerCompleted(IAsyncResult asynResult) { IsHandleFinished = true; // 如果超时了,直接退出 if (IsHandlerTimeOuts) { return; } if (IsStop) { (asynResult.AsyncState as HandlerNode).nodeHandler.EndInvoke(asynResult); resetEvent.Set(); return; } if (asynResult == null) { return; } bool flag = (asynResult.AsyncState as HandlerNode).nodeHandler.EndInvoke(asynResult); if (flag == true) { // PrintLog("将当前执行单元指向下一个..."); curExcute = curExcute.next; isFirstTimeExcuteNode = true; currentRepeatTimes = 0; } else { currentRepeatTimes++; isFirstTimeExcuteNode = false; } // PrintLog("子线程完成执行单元任务...."); resetEvent.Set(); }
private void ExcuteProxySyn(HandlerNode excutor) { bool suc = excutor.nodeHandler.Invoke(this, null); if (suc) { PrintLog("将当前执行单元指向下一个..."); curExcute = curExcute.next; resetEvent.Set(); } }
private void ExcuteProxy(HandlerNode excutor) { currentThread = new Thread(new ThreadStart(delegate(){ object obj = new object(); asyncResult = excutor.nodeHandler.BeginInvoke(this, obj, HandlerCompleted, curExcute); })); currentThread.Start(); }
/// <summary> /// 开始执行 /// </summary> public void Excute() { IsStop = false; allFinished = false; // 初试变量 curExcute = header.next; timer = new System.Timers.Timer(HandlerTimeOuts); timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); while (!IsStop) { try { if (conditionNode != null && !conditionNode.Invoke(null, null)) { WaitForTimes(1000); PrintLog("等待条件完成..."); continue; } WaitForTimes(500); if (null == curExcute || curExcute.nodeHandler == null) { PrintLog("处理单元为空!"); break; } resetEvent.Reset(); // 等待 if (IsUsedGoto) { curExcute = handleMap[gotoHandleName]; PrintLog("跳转到流程:" + curExcute.handlerName); PrintLog("【"+curExcute.handlerName + "】开始: " + timeOutsOfGoto.ToString()); WaitForTimes(timeOutsOfGoto); IsUsedGoto = false; } else { PrintLog("【"+curExcute.handlerName + "】开始: " + curExcute.nextTimeOuts.ToString()); WaitForTimes(curExcute.nextTimeOuts); } // 调用预处理 if (preNode != null) { preNode.Invoke(null, null); } // 如果是第一次执行该node,则调用FirstTimeExcute Node if (isFirstTimeExcuteNode) { // 如果存在第一次执行节点时,需要执行的节点,调用该节点 if (nodeAtFirstTime != null) { nodeAtFirstTime.Invoke(null, null); } currentRepeatTimes = 0; } string currentHandlerName = curExcute.handlerName; if (currentRepeatTimes >= repeatTimes && endExcuteNodeName != "") { PrintLog("【" + curExcute.handlerName + "】重复执行,超过 " + currentRepeatTimes.ToString()+" 次"); GotoHanlderUnit(endExcuteNodeName, 1000); continue; } // 开始计时,如果任务在HandlerTimeOut时间间隔内没完成则超时 IsHandlerTimeOuts = false; IsHandleFinished = false; timer.Enabled = true; timer.Start(); asyncResult = null; if (isAsyn) ExcuteProxy(curExcute); else ExcuteProxySyn(curExcute); // 调用后处理 if (postNode != null) { postNode.Invoke(null, null); } resetEvent.WaitOne(); timer.Stop(); if (IsHandlerTimeOuts) { PrintLog("【" + currentHandlerName + "】 运行时间超时,关闭重来"); GotoHanlderUnit(endExcuteNodeName, 1000); isFirstTimeExcuteNode = true; currentRepeatTimes = 0; } else { PrintLog("【" + currentHandlerName + "】完成"); } } catch (System.Exception ex) { PrintLog("出现异常..." + ex.StackTrace); } } allFinished = true; PrintLog("处理流程已经停止!"); }
/// <summary> /// 添加处理单元 /// </summary> /// <param name="handler"></param> /// <param name="name"></param> /// <param name="timeOuts"></param> /// <returns></returns> public bool AddHandler(AccessHandler handler, string name = null, int timeOuts = 1000) { MethodInfo method = handler.Method; string mName = name; if (name == null) { mName = method.Name; } if (handleMap.ContainsKey(mName)) { PrintLog("已经具有同名函数", currentLogType); return false; } timeOuts += RandomGenerator.getRandom().Next(100); HandlerNode cur = new HandlerNode(priviousNode, handler, mName, timeOuts); handleMap.Add(mName, cur); priviousNode = cur; // priviousNode.next = header.next; return true; }
public UIAccessProcessor(LogHandler log) { header = new HandlerNode(); priviousNode = header; printLog = log; IsStop = false; conditionNode = preNode = postNode = nodeAtFirstTime = null; HandlerTimeOuts = 5000; }