void AutomationDone(object stateObject) { lock (_lock) { BatchTaskUnit unit = stateObject as BatchTaskUnit; if (unit != null) { unit.UnitState = BatchTaskUnit.State.Done; _workingUnitCount--; ++_completeServerCount; } } }
/// <summary> /// 添加服务器 /// </summary> /// <param name="serverId"></param> public void AddServer(int serverId) { if (_state == State.Doing) { throw new InvalidOperationException(); } for (int i = 0; i < _taskUnitList.Count; i++) { if (_taskUnitList[i].ServerId == serverId) { return; } } BatchTaskUnit unit = new BatchTaskUnit(); unit.ServerId = serverId; unit.UnitState = BatchTaskUnit.State.Querying; _taskUnitList.Add(unit); }
/// <summary> /// 执行任务 /// </summary> /// <remarks>耗时的操作</remarks> void DoTask() { while (!_needAbort && _state != State.Done) { lock (_lock) { if (_state == State.Doing) { if (_needPause) { _state = State.Paused; _needPause = false; } else { if (_workingUnitCount < _step) { if (_nextUnitIndex < _taskUnitList.Count) { BatchTaskUnit unit = _taskUnitList[_nextUnitIndex]; if (unit.UnitState == BatchTaskUnit.State.Querying) { GameServer server = AdminServer.TheInstance.GameServerManager.GetGameServer(unit.ServerId); if (server != null) { unit.UnitState = BatchTaskUnit.State.Doing; _workingUnitCount++; AutomationContext context = new AutomationContext(); context.ServerList.Add(server); AdminServer.TheInstance.AutomationManager.Do(Automation, context, new DelegateAutomationDone(AutomationDone), unit); } else { unit.UnitState = BatchTaskUnit.State.Done; } } _nextUnitIndex++; } else if (_workingUnitCount == 0) { _state = State.Done; } } } } else if (_state == State.Paused) { if (_needContinue) { _state = State.Doing; _needContinue = false; } else { Thread.Sleep(1000); } } } Thread.Sleep(1); } if (_needAbort) { _state = State.Aborted; _needAbort = false; } _endTime = DateTime.Now; }
/// <summary> /// 添加服务器 /// </summary> /// <param name="serverId"></param> public void AddServer(int serverId) { if (_state == State.Doing) throw new InvalidOperationException(); for (int i = 0; i < _taskUnitList.Count; i++) { if (_taskUnitList[i].ServerId == serverId) { return; } } BatchTaskUnit unit = new BatchTaskUnit(); unit.ServerId = serverId; unit.UnitState = BatchTaskUnit.State.Querying; _taskUnitList.Add(unit); }