public async Task ExecTest() { var que = new TaskQueue <bool>(); //失败重试 que.Add(_ => Run(1000), 0, "cmd1"); que.Add(_ => Run(500, false), 2, "cmd2"); que.Add(_ => Run(500), 0, "cmd3"); await que.Exec(); que.History.Select(p => p.Result).Should().BeEquivalentTo(new[] { true, false, true }); //固定超时 que.Clear(); que.Add(token => Run(1000, true, token), 0, "cmd1"); que.Add(token => Run(1000, true, token), 0, "cmd2"); que.Add(token => Run(1000, true, token), 0, "cmd3"); await Assert.ThrowsAsync(typeof(OperationCanceledException), async() => await que.Exec(1.7)); que.History.Count(p => p.Result).Should().Be(2); //取消任务 que.Clear(); que.Add(token => Run(500, true, token), 0, "cmd1"); que.Add(token => Run(500, true, token), 0, "cmd2"); que.Add(token => Run(500, true, token), 0, "cmd3"); que.Add(token => Run(500, true, token), 0, "cmd4"); que.Add(token => Run(500, true, token), 0, "cmd5"); que.Add(token => Run(500, true, token), 0, "cmd6"); var cts = new CancellationTokenSource(); cts.CancelAfter(2100); await Assert.ThrowsAsync(typeof(OperationCanceledException), async() => await que.Exec(cts.Token)); que.History.Count().Should().Be(5); //暂停 继续 que.Build(); await Assert.ThrowsAsync(typeof(OperationCanceledException), async() => await que.Exec(1.2)); que.History.Count(p => p.Result).Should().Be(2); que.History.Count(p => !p.Result).Should().Be(1); que.PendingQueue.Count().Should().Be(4); await que.Exec(); que.History.Count(p => p.Result).Should().Be(6); que.History.Count(p => !p.Result).Should().Be(1); que.PendingQueue.Count().Should().Be(0); }
void Update() { if (shouldReloadMainXML) { shouldReloadMainXML = false; #if UNITY_EDITOR ReloadCanvas(); #endif } lock (_queueLock) { // allow us to process tasks for a number of milliseconds before holding off until later Stopwatch sw = new Stopwatch(); sw.Start(); // make a copy of the queue and process the copy; this will allow callee's to queue tasks correctly List <Task> currentQueue = new List <Task> (TaskQueue); TaskQueue.Clear(); while (currentQueue.Count > 0 && sw.ElapsedMilliseconds < 60) { currentQueue [0] (); currentQueue.RemoveAt(0); } // for any tasks which did not get executed, add them back to the front of the task queue for (int i = currentQueue.Count - 1; i >= 0; i--) { TaskQueue.Insert(0, currentQueue [i]); } } }
public virtual void Do(Dictionary <MyTuple <IntelItemType, long>, IFleetIntelligence> IntelItems, TimeSpan canonicalTime, Profiler profiler) { while (true) { if (TaskQueue.Count == 0) { return; } var task = TaskQueue.Peek(); task.Do(IntelItems, canonicalTime, profiler); if (task.Status == TaskStatus.Complete) { TaskQueue.Dequeue(); } else if (task.Status == TaskStatus.Aborted && !ContinueOnAbort) { TaskQueue.Clear(); Aborted = true; break; } else if (task.Status == TaskStatus.Aborted && ContinueOnAbort) { TaskQueue.Dequeue(); } else { break; } } }
public void Stop() { OutputWriter.WriteLine("Stopping all background tasks"); TaskQueue.Clear(); if (CurrentTask != null) { CurrentTask.Cancel(); } }
public override void Do(Dictionary <MyTuple <IntelItemType, long>, IFleetIntelligence> IntelItems, TimeSpan canonicalTime, Profiler profiler) { if (!IntelItems.ContainsKey(IntelKey)) { Aborted = true; TaskQueue.Clear(); return; } base.Do(IntelItems, canonicalTime, profiler); }
public void AddTask(TaskType taskType, MyTuple <IntelItemType, long> intelKey, CommandType commandType, int arguments, TimeSpan canonicalTime) { if (commandType == CommandType.Override) { TaskQueue.Clear(); } if (TaskGenerators.ContainsKey(taskType)) { ITask Task = TaskGenerators[taskType].GenerateTask(taskType, intelKey, IntelProvider.GetFleetIntelligences(canonicalTime - IntelProvider.CanonicalTimeDiff), canonicalTime, Context.Reference.CubeGrid.EntityId); if (Task is NullTask) { return; } TaskQueue.Enqueue(Task); } }
public override void Do(Dictionary <MyTuple <IntelItemType, long>, IFleetIntelligence> IntelItems, TimeSpan canonicalTime, Profiler profiler) { if (!IntelItems.ContainsKey(IntelKey)) { Aborted = true; TaskQueue.Clear(); return; } DockIntel dock = (DockIntel)IntelItems[IntelKey]; Vector3D dockPosition = dock.GetPositionFromCanonicalTime(canonicalTime); Vector3 approachPoint = dock.WorldMatrix.Forward * dock.UndockFar + dockPosition; Vector3 entryPoint = dock.WorldMatrix.Forward * (dock.UndockNear + (DockSize == MyCubeSize.Large ? 1.25f : 0.5f) + 1) + dockPosition; Vector3 closePoint = dock.WorldMatrix.Forward * (dock.UndockNear + (DockSize == MyCubeSize.Large ? 1f : 0.25f)) + dockPosition; Vector3 dockDirection = dock.WorldMatrix.Backward; Vector3D dockToMeDir = Program.Me.WorldMatrix.Translation - dockPosition; double dockToMeDist = dockToMeDir.Length(); ApproachEntrance.Destination.Direction = dockDirection; if (dockToMeDist < 250 && dockToMeDist > 150) { EnterHoldingPattern.Destination.Position = Vector3D.Zero; } else { dockToMeDir.Normalize(); Vector3 holdPoint = dockToMeDir * 200 + dockPosition; EnterHoldingPattern.Destination.Position = holdPoint; EnterHoldingPattern.Destination.Velocity = dock.GetVelocity(); } ApproachEntrance.Destination.Direction = dockDirection; ApproachEntrance.Destination.Position = approachPoint; ApproachEntrance.Destination.Velocity = dock.GetVelocity(); ApproachDock.Destination.Direction = dockDirection; ApproachDock.Destination.Position = entryPoint; ApproachDock.Destination.Velocity = dock.GetVelocity(); FinalAdjustToDock.Destination.Direction = dockDirection; FinalAdjustToDock.Destination.Position = closePoint; FinalAdjustToDock.Destination.Velocity = dock.GetVelocity(); if (Indicator != null && dock.IndicatorDir != Vector3D.Zero) { //Matrix.CreateWorld(Connector.WorldMatrix.Translation, Connector.WorldMatrix.Forward, Indicator.WorldMatrix.Forward); var tDir = Vector3D.TransformNormal(Vector3D.TransformNormal(dock.IndicatorDir, MatrixD.Transpose(MatrixD.CreateFromDir(Connector.WorldMatrix.Forward, Indicator.WorldMatrix.Forward))), Connector.WorldMatrix); ApproachEntrance.Destination.DirectionUp = tDir; ApproachDock.Destination.DirectionUp = tDir; FinalAdjustToDock.Destination.DirectionUp = tDir; } if (TaskQueue.Count < 6) { Program.IGC.SendBroadcastMessage(dock.HangarChannelTag, MyTuple.Create(Program.Me.CubeGrid.EntityId, dock.ID, (int)HangarRequest.RequestDock)); Program.IGC.SendBroadcastMessage(dock.HangarChannelTag, MyTuple.Create(Program.Me.CubeGrid.EntityId, dock.ID, (int)HangarRequest.Reserve)); if (dock.OwnerID == Program.Me.CubeGrid.EntityId && (dock.Status & HangarStatus.Docking) != 0) { WaitForClearance.Status = TaskStatus.Complete; } } if (TaskQueue.Count == 4 && (ApproachEntrance.Autopilot.Reference.GetPosition() - approachPoint).LengthSquared() < 1) { TaskQueue.Dequeue(); } if (TaskQueue.Count == 3 && (ApproachDock.Autopilot.Reference.GetPosition() - entryPoint).LengthSquared() < 0.5) { TaskQueue.Dequeue(); } if (TaskQueue.Count < 3) { if (DockTask.DockingSubsystem.Connector.Status == MyShipConnectorStatus.Connectable) { FinalAdjustToDock.Autopilot.Clear(); DockTask.Do(IntelItems, canonicalTime, profiler); TaskQueue.Clear(); } } base.Do(IntelItems, canonicalTime, profiler); }