public void SendToChild(Action <AsyncJob> act) { if (this._circle.Count < 1) { throw new InvalidOperationException("没有子工作时无法使用SendToChild方法!"); } if (this._currentInputChild == null) { this._currentInputChild = this._circle.Head; } this._currentInputChild.In(act); this._currentInputChild = this._currentInputChild.Next as AsyncJob; }
/// <summary> /// 生成多层次异步操作的实例 /// </summary> public AsyncJob() { this._inputQueue = new ConcurrentQueue <Action <AsyncJob> >(); this._outputQueue = new ConcurrentQueue <Action>(); this._circle = new Circle <AsyncJob>(); this.OutBufferLength = 10; this.InBufferLength = 10; this.Last = null; this.Next = null; this._inputEnd = false; this._inTaskCompleted = false; this._outTaskCompleted = false; this._currentInputChild = _circle.Head; }
/// <summary> /// 启动事件(包括所有下层事件) /// </summary> public void Start() { _inTask = new Task(() => { Action <AsyncJob> act; while (!_inputEnd) { if (this._inputQueue.TryDequeue(out act)) { act(this); } else { Thread.Sleep(20); } } if (this._circle.Count > 0) { AsyncJob a = this._circle.Head; a.In(j => { j.EndInput(); }); while (a.Next != this._circle.Head) { a = a.Next as AsyncJob; a.In(j => { j.EndInput(); }); } } this._inTaskCompleted = true; }); _outTask = new Task(() => { while (this._circle.Count > 0) { if (this._currentOutputChild == null) { this._currentOutputChild = this._circle.Head; } Action act = _currentOutputChild.PopCallBack(); if (act != null) { this._outputQueue.Enqueue(act); this._currentOutputChild = this._currentOutputChild.Next as AsyncJob; } else { if (this._currentOutputChild.IsCompleted) { this._circle.Remove(this._currentOutputChild); this._currentOutputChild = this._currentOutputChild.Next as AsyncJob; } else { Thread.Sleep(20); this._currentOutputChild = this._currentOutputChild.Next as AsyncJob; } } } this._outTaskCompleted = true; }); _inTask.Start(); _outTask.Start(); foreach (AsyncJob e in this._circle) { e.Start(); } }
/// <summary> /// 添加子节点,所有的子节点以环形结构相连 /// </summary> /// <param name="childJob"></param> public void addChild(AsyncJob childJob) { this._circle.Add(childJob); }