Exemplo n.º 1
0
        private void RunGuardLoop()
        {
            int errorCount = 0;

            while (true)
            {
                lock (_ctsLock)
                {
                    if (_cts == null)
                    {
                        LogInfo("NotStarted");
                        break;
                    }
                    if (_cts.IsCancellationRequested)
                    {
                        LogInfo("Cancelled");
                        break;
                    }
                }

                try
                {
                    LoopAction?.Invoke();
                    LoopTask?.Invoke().Wait();
                }
                catch (Exception e)
                {
                    errorCount++;
                    if (errorCount > MaxTryFailCount)
                    {
                        LogInfo(string.Format("fail {0} more then max: {1}, exit", errorCount, MaxTryFailCount));
                        break;
                    }
                    LogEx(e, string.Format("fail time: {0}/{1}, ex:{2}", errorCount, MaxTryFailCount, e.Message));
                }

                try
                {
                    //Thread.Sleep actually makes current thread to sleep
                    //Task.Delay is a logical delay without blocking the current thread. Task.Delay is a timer based wait mechanism.
                    //In async programming model you should always use Task.Delay() if you want something(continuation) happen after some delay.
                    TaskEx.Delay(LoopSpan).Wait();
                }
                catch (TaskCanceledException)
                {
                    //await TaskEx.Delay(LoopSpan); should enter here
                    LogInfo(string.Format("Task Canceled => TaskCanceledException"));
                }
                catch (AggregateException)
                {
                    //TaskEx.Delay(LoopSpan).Wait(); should enter here
                    LogInfo(string.Format("Task Canceled => AggregateException"));
                }
                catch (Exception)
                {
                    //should never enter here
                    LogInfo(string.Format("Task Canceled => Exception"));
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Wait for message with requested type in 'this' queue
 /// </summary>
 /// <returns>first message in queue if it matches for request type. Null if exited by condition</returns>
 public T WaitForMessage(Type type, FinishCondition finish = null, LoopAction action = null)
 {
     while (true)
     {
         if (finish != null && finish())
         {
             return(null);
         }
         if (action != null)
         {
             action.Execute();
         }
         CloudQueueMessage startMsg = GetMessage();
         if (startMsg == null)
         {
             System.Threading.Thread.Sleep(1000);
             continue;
         }
         DeleteMessage(startMsg);
         T message = ConvertMessage(startMsg);
         if (message.GetType() == type)
         {
             return(message);
         }
     }
 }
Exemplo n.º 3
0
        public void Add(int?item)
        {
            LoopAction loopAction = Add;

            if (!GameBoardLooper(item, loopAction))
            {
                throw new ArgumentException("GameBoard Is full");
            }
        }
Exemplo n.º 4
0
            public Action Handle()
            {
                LoopAction self = this;

                return(() =>
                {
                    self.action();
                    ScheduleManager.Schedule(self.time, self.Handle());
                });
            }
Exemplo n.º 5
0
    public static LoopAction NewObject()
    {
        GameObject pack = new GameObject {
            name = "Loop"
        };
        LoopAction   actionpack = pack.AddComponent <LoopAction>();
        ActionPlayer player     = pack.AddComponent <ActionPlayer>();
        ActionList   list       = pack.AddComponent <ActionList>();

        actionpack.PackPlayer  = player;
        actionpack.PackActions = list;
        actionpack.name        = "循环";
        return(actionpack);
    }
Exemplo n.º 6
0
 private bool GameBoardLooper(int?item, LoopAction loopAction)
 {
     for (int x = 0; x < _gameBoardRoot; x++)
     {
         for (int y = 0; y < _gameBoardRoot; y++)
         {
             if (loopAction(item, x, y))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemplo n.º 7
0
        public void WaitForWorkers(int workersCount, MessageQueue <DispetcherMessage> dispatcherHandler,
                                   string sessionGuid, int sceneID, LoopAction action)
        {
            MessageQueue <RenderMessage> renderHandler = new MessageQueue <RenderMessage>();

            for (int i = 0; i < workersCount; ++i)
            {
                IsFreeMessage isFreeMes = new IsFreeMessage(sessionGuid, sceneID); //send check message to worker
                renderHandler.AddMessage(isFreeMes);
            }
            int workersReadyCount = 0;

            while (workersReadyCount < workersCount)
            {
                DispetcherMessage dispMessage = dispatcherHandler.WaitForMessage(typeof(WorkerIsReady), null, action);
                ++workersReadyCount;
            }
        }
Exemplo n.º 8
0
        private void RunGuardLoop()
        {
            int errorCount = 0;

            while (true)
            {
                lock (_ctsLock)
                {
                    if (_cts == null)
                    {
                        LogMessage("NotStarted");
                        break;
                    }
                    if (_cts.IsCancellationRequested)
                    {
                        LogMessage("Cancelled");
                        break;
                    }
                }

                try
                {
                    LoopAction?.Invoke();
                    LoopTask?.Invoke().Wait();
                }
                catch (Exception e)
                {
                    errorCount++;
                    if (errorCount > MaxTryFailCount)
                    {
                        LogMessage(string.Format("fail {0} more then max: {1}, exit", errorCount, MaxTryFailCount));
                        break;
                    }
                    LogMessage(string.Format("fail time: {0}/{1}, ex:{2}", errorCount, MaxTryFailCount, e.Message));
                }

                Task.Delay(LoopSpan).Wait();
            }
        }
Exemplo n.º 9
0
 static unsafe ThreadWrapper()
 {
     s_pause = GetPauseDelegate();
 }
Exemplo n.º 10
0
        public bool Remove(int?item)
        {
            LoopAction loopAction = Remove;

            return(GameBoardLooper(item, loopAction));
        }
 internal CombinatorialUtils(LoopAction loopAction)
 {
     this.loopAction = loopAction;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Implementors should call this so the main loop has something
 /// to do.
 /// </summary>
 /// <param name="p_la">LoopAction method to run at each iteration</param>
 static public void SetAndStartAction(Control control, LoopAction p_la)
 {
     SetAction(control, p_la);
     Start();
 }
Exemplo n.º 13
0
        public static void ScheduleInLoop(float timer, Action action)
        {
            LoopAction loop = new LoopAction(timer, action);

            Schedule(timer, loop.Handle());
        }
Exemplo n.º 14
0
 static public void SetAction(Control control, LoopAction p_la)
 {
     LoopControl.control = control;
     control.Disposed   += control_Disposed;
     loopAction          = p_la;
 }
Exemplo n.º 15
0
        private static LoopAction loopAction; // our ptr to the function called in each frame iteration

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Constructor sets Application.Idle handler
        /// </summary>
        static LoopControl()
        {
            loopAction = DefaultAction;
            fpsTimer = null;
        }
Exemplo n.º 16
0
 public static void Start()
 {
     Application.Idle += new EventHandler( Application_Idle );
     loopAction = loopAction ?? DefaultAction;
 }
Exemplo n.º 17
0
 /// <summary>
 /// Implementors should call this so the main loop has something
 /// to do.
 /// </summary>
 /// <param name="p_la">LoopAction method to run at each iteration</param>
 public static void SetAndStartAction( Control control, LoopAction p_la )
 {
     SetAction( control, p_la );
     Start();
 }
Exemplo n.º 18
0
        public bool Contains(int?item)
        {
            LoopAction loopAction = Contains;

            return(GameBoardLooper(item, loopAction));
        }
Exemplo n.º 19
0
        public void WaitForMerger(MessageQueue <DispetcherMessage> dispatcherHandler, int sceneID, LoopAction action)
        {
            DispetcherMessage dispMessage = dispatcherHandler.WaitForMessage(typeof(MergerIsReady), null, action);
            MergerIsReady     go          = dispMessage as MergerIsReady;

            if (go.MergerQueueSuffix != sceneID.ToString())  //check for thread error
            {
                m_log.Error("Unknown error while waiting for 'MergerIsReady' message. ID: " + sceneID.ToString());
                throw (new Exception("Unknown error while waiting for 'MergerIsReady' message"));
            }
        }
Exemplo n.º 20
0
 public static void Start()
 {
     Application.Idle += new EventHandler(Application_Idle);
     loopAction        = loopAction ?? DefaultAction;
 }
Exemplo n.º 21
0
 /// <summary>
 /// Constructor sets Application.Idle handler
 /// </summary>
 static LoopControl()
 {
     loopAction = DefaultAction;
     fpsTimer   = null;
 }
Exemplo n.º 22
0
    public static void AddLoop()
    {
        ActionPack newpack = LoopAction.NewObject();

        Set2Object(newpack.gameObject);
    }
Exemplo n.º 23
0
 public static void SetAction( Control control, LoopAction p_la )
 {
     LoopControl.control = control;
     control.Disposed += control_Disposed;
     loopAction = p_la;
 }