Ejemplo n.º 1
0
        public void work()
        {
            lock (Worker._lock)
            {
                Worker.nThreadCount++;
                this.nThreadOrder = Worker.nThreadCount;
            }
            TaskWrapper taskWrapper   = null;
            int         lastTickCount = int.MinValue;

            while (!Program.NeedExitServer)
            {
                int tickCount = Environment.TickCount;
                if (tickCount <= lastTickCount + 5)
                {
                    if (lastTickCount <= 0 || tickCount >= 0)
                    {
                        Thread.Sleep(5);
                        continue;
                    }
                }
                lastTickCount = tickCount;
                long ticks = TimeUtil.NOW();
                for (;;)
                {
                    try
                    {
                        taskWrapper = this.getCanExecuteTask(ticks);
                        if (taskWrapper == null || null == taskWrapper.CurrentTask)
                        {
                            break;
                        }
                        if (taskWrapper.canExecute)
                        {
                            try
                            {
                                taskWrapper.CurrentTask.run();
                            }
                            catch (Exception ex)
                            {
                                DataHelper.WriteFormatExceptionLog(ex, "异步调度任务执行异常", false, false);
                            }
                        }
                        if (taskWrapper.Periodic > 0L && taskWrapper.canExecute)
                        {
                            taskWrapper.resetStartTime();
                            this.executor.addTask(taskWrapper);
                            taskWrapper.addExecuteCount();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            SysConOut.WriteLine(string.Format("ScheduleTask Worker{0}退出...", this.nThreadOrder));
        }
Ejemplo n.º 2
0
        private static void OnTimedEvent(object source)
        {
            ScheduleTask task = source as ScheduleTask;

            if (task is MonsterTask)
            {
                MonsterTask monsterTask = task as MonsterTask;
                if (monsterTask.mapCode == 1)
                {
                    long ticks1 = TimeUtil.NOW();
                    SysConOut.WriteLine("----------------------------------时间触发器时间间隔----------------------------" + (ticks1 - ScheduleExecutor2.Instance.Oldticks).ToString());
                    ScheduleExecutor2.Instance.Oldticks = ticks1;
                }
            }
            if (task.InternalLock.TryEnter())
            {
                bool logRunTime = false;
                long nowTicks   = TimeUtil.CurrentTicksInexact;
                try
                {
                    task.run();
                }
                catch (System.Exception ex)
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时异常,{1}", task.ToString(), ex.ToString()));
                }
                finally
                {
                    logRunTime = task.InternalLock.Leave();
                }

                if (logRunTime)
                {
                    long finishTicks = TimeUtil.CurrentTicksInexact;
                    if (finishTicks - nowTicks > TimeUtil.SECOND)
                    {
                        try
                        {
                            MonsterTask monsterTask = task as MonsterTask;
                            if (null != monsterTask)
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("{0} mapCode:{1},subMapCode:{2},执行时间:{3}毫秒"
                                                                                  , task.ToString(), monsterTask.mapCode, monsterTask.subMapCode, finishTicks - nowTicks));
                            }
                            else
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时间:{1}毫秒", task.ToString(), finishTicks - nowTicks));
                            }
                        }
                        catch
                        {
                            //写日志异常就不记了
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void ThreadProc()
        {
            Stack <ScheduleTask> stack = new Stack <ScheduleTask>();

            for (;;)
            {
                long nowTicks = TimeUtil.NOW();
                try
                {
                    stack.Clear();
                    lock (this.Mutex)
                    {
                        for (int i = 0; i < this.TaskList.Count; i++)
                        {
                            if (nowTicks >= this.TaskList[i].NextTicks)
                            {
                                this.TaskList[i].NextTicks = nowTicks + this.TaskList[i].Periodic;
                                ScheduleTask task = this.TaskList[i].Task;
                                stack.Push(task);
                            }
                        }
                    }
                    foreach (ScheduleTask task in stack)
                    {
                        try
                        {
                            task.run();
                        }
                        catch (Exception ex)
                        {
                            LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时异常,{1}", task.ToString(), ex.ToString()), null, true);
                        }
                        long finishTicks = TimeUtil.CurrentTicksInexact;
                        if (finishTicks - nowTicks > 1000L)
                        {
                            LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时间:{1}毫秒", task.ToString(), finishTicks - nowTicks), null, true);
                        }
                    }
                }
                catch
                {
                }
                int sleepMs = Math.Max(0, (int)Math.Min(TimeUtil.NOW() + 250L - nowTicks, 250L));
                Thread.Sleep(sleepMs);
            }
        }
Ejemplo n.º 4
0
 public TaskWrapper(ScheduleTask task, long delay, long periodic)
 {
     this.currentTask = task;
     this.startTime   = TimeUtil.NOW() + delay;
     this.periodic    = periodic;
 }
Ejemplo n.º 5
0
        public void work()
        {
            lock (_lock)
            {
                nThreadCount++;
                nThreadOrder = nThreadCount;
            }

            TaskWrapper taskWrapper = null;

            int lastTickCount = int.MinValue;

            while (!Program.NeedExitServer)
            {
                //检索可执行的任务
                int tickCount = Environment.TickCount;
                if (tickCount <= lastTickCount + 5)
                {
                    if (lastTickCount <= 0 || tickCount >= 0) //考虑当打到int最大值时的情况
                    {
                        Thread.Sleep(5);
                        continue;
                    }
                }
                lastTickCount = tickCount;

                long ticks = TimeUtil.NOW();
                while (true)
                {
                    try
                    {
                        taskWrapper = getCanExecuteTask(ticks);
                        if (null == taskWrapper || null == taskWrapper.CurrentTask)
                        {
                            break;
                        }

                        if (taskWrapper.canExecute)
                        {
                            try
                            {
                                taskWrapper.CurrentTask.run();
                            }
                            catch (System.Exception ex)
                            {
                                DataHelper.WriteFormatExceptionLog(ex, "异步调度任务执行异常", false);
                            }
                        }

                        //如果是周期执行的任务
                        if (taskWrapper.Periodic > 0 && taskWrapper.canExecute)
                        {
                            //设置下一次执行的时间
                            taskWrapper.resetStartTime();
                            executor.addTask(taskWrapper);
                            taskWrapper.addExecuteCount();
                        }
                    }
                    catch (System.Exception /* ex*/)
                    {
                        //LogManager.WriteLog(LogTypes.Error, string.Format("异步调度任务执行错误: {0}", ex));
                    }
                }
            }

            System.Console.WriteLine(string.Format("ScheduleTask Worker{0}退出...", nThreadOrder));
        }