/// <summary>
        /// Process the queue when a task was pushed in
        /// do the task and remove it from queue if it is done
        /// or cancel if it try to do it three times
        /// </summary>
        private static async void ProcessQueue()
        {
            await Mutex.WaitAsync();

            try
            {
                int reDoWorkCounter = 0;

                while (TaskQueue.Count >= 1)
                {
                    var taskFormQueue = TaskQueue.Peek();
                    reDoWorkCounter++;
                    taskFormQueue?.Start();
                    var success = taskFormQueue.Result;
                    if (success || reDoWorkCounter >= 3)
                    {
                        var removeTask = TaskQueue.Dequeue();
                        removeTask      = null;
                        reDoWorkCounter = 0;
                    }
                }
            }
            catch
            {
            }
            finally
            {
                Mutex.Release();
            }
        }
Example #2
0
 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;
         }
     }
 }
 private async Task DoDequeueLoop()
 {
     IsRunning = true;
     while (TaskQueue.Any())
     {
         if (CurrentTask == null)
         {
             CurrentTask = TaskQueue.Dequeue();
             try
             {
                 CurrentTask.StartAsync().GetAwaiter();
             }
             catch (Exception ex)
             {
                 throw new Exception("Error running queue", ex);
             }
         }
         else
         {
             if (!CurrentTask.IsRunning)
             {
                 CurrentTask = null;
             }
         }
         await Task.Delay(200);
     }
     IsRunning = false;
 }
Example #4
0
        public TaskQueueTestFixture(string uniqueId, TaskQueue taskQueue = null)
        {
            _semaphoreFile = Path.Combine(AppContext.BaseDirectory, uniqueId, Path.GetTempFileName());

            var testQueueName = uniqueId + "::TestQueue";

            TaskQueue = taskQueue ?? TaskQueue.Redis(RedisConnectionString, testQueueName);

            // Clear out the queue
            while (TaskQueue.Dequeue() != null)
            {
            }
        }
Example #5
0
    public static void Run()
    {
        var start = Utils.ReadMicroseconds();

        do
        {
            var task = queue.Dequeue();
            if (task == null)
            {
                break;
            }
            task.Run();
        } while ((Utils.ReadMicroseconds() - start) < maxFrameTimeMicroseconds);
    }
Example #6
0
    void Update()
    {
        if (shouldReloadMainXML)
        {
            shouldReloadMainXML = false;
                        #if UNITY_EDITOR
            ReloadCanvas();
                        #endif
        }

        lock (_queueLock)
        {
            if (TaskQueue.Count > 0)
            {
                TaskQueue.Dequeue()();
            }
        }
    }
Example #7
0
        private void ThreadWork(object obj)
        {
            WaitHandle[] waitHandles = { _taskAvailable, _eventStop };

            while (true)
            {
                int index = WaitHandle.WaitAny(waitHandles);

                if (waitHandles[index] == _eventStop)
                {
                    return;
                }

                Task task;

                lock (_tasks)
                {
                    Debug.Assert(_tasks.Count != 0);
                    task = _tasks.Dequeue();
                    _workTask++;
                }

                try
                {
                    task.Run();
                }
                catch (Exception e)
                {
                    // keep the first exception
                    Interlocked.CompareExchange(ref _unHandledException, e, null);
                }

                lock (_tasks)
                {
                    _workTask--;
                    if (_tasks.Count == 0 && _workTask == 0)
                    {
                        _eventIdle.Set();
                    }
                }
            }
        }
Example #8
0
        public string GetNextTask()
        {
            //var commonds = SampleTestTask();
            //JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
            //string serialized = JsonConvert.SerializeObject(commonds, settings);

            //return serialized;

            if (TaskQueue.Any())
            {
                var commonds = TaskQueue.Dequeue();

                JsonSerializerSettings settings = new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.Auto
                };
                string serialized = JsonConvert.SerializeObject(commonds, settings);

                return(serialized);
            }
            else
            {
                return(string.Empty);
            }
        }
Example #9
0
        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);
        }
Example #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="taskCollection"></param>
        private void DoTasks(TaskQueue tasks)
        {
            if (tasks.Count == 0)
            {
                return;
            }

            TaskCollection tempTasks = new TaskCollection();

            while (tasks.Count > 0)
            {
                ITask headTask = tasks.Dequeue();

                bool b = DoNotExecutingTask(headTask);
                if (b)
                {
                    break;
                }
                else
                {
                    tempTasks.Add(headTask);
                }
            }

            //
            //
            tasks.Enqueue(tempTasks);
        }