protected void TryExecuteTask()
 {
     if (g_QueueTasks.Count == 0)
     {
         return;
     }
     lock (lockObj)
     {
         for (int i = 0; i < m_lstThread.Count; i++)
         {
             if (!m_lstThread[i].IsIdle)
             {
                 continue;
             }
             WorkTask task = null;
             if (!TryDispatchWorkTask(out task))
             {
                 break;
             }
             if (!m_lstThread[i].Start(task))
             {
                 g_QueueTasks.Enqueue(task);
             }
         }
     }
 }
 public bool Equals(WorkTask task)
 {
     if (task != null && task.Name == this.Name)
     {
         return(true);
     }
     return(false);
 }
 /// <summary>
 /// 尝试将以前排队到此计划程序中的WorkTask取消排队
 /// </summary>
 /// <param name="task"></param>
 /// <returns></returns>
 protected internal virtual bool TryDequeue(WorkTask task)
 {
     if (task.IsAlive)
     {
         task.Status = TaskStatus.Canceled;
         return(true);
     }
     return(false);
 }
        public static WorkTask CreateTask <TResult>(Func <TResult> method)
        {
            WorkTask task = new WorkTask(method);

            task.DynamicInvoke = (delegate()
            {
                task.ReturnValue = method();
            });
            return(task);
        }
        public static WorkTask CreateTask(Action method)
        {
            WorkTask task = new WorkTask(method);

            task.DynamicInvoke = (delegate()
            {
                method();
            });
            return(task);
        }
        public static WorkTask CreateTask(Delegate method, params object[] args)
        {
            WorkTask task = new WorkTask(method, args);

            task.DynamicInvoke = (delegate()
            {
                task.ReturnValue = method.DynamicInvoke(task.MethodArgs);
            });
            return(task);
        }
 /// <summary>
 /// 尝试在此计划程序上执行提供的WorkTask
 /// </summary>
 /// <param name="task"></param>
 /// <returns></returns>
 protected bool TryExecuteTask(WorkTask task)
 {
     lock (lockObj)
     {
         var thread = OnDispatchWorkThread();
         if (thread != null && thread.Start(task))
         {
             return(true);
         }
         return(false);
     }
 }
        private void Working()
        {
            try
            {
                do
                {
                    m_IsWaiting      = false;
                    this.LastChanged = DateTime.Now;
                    if (this.Task != null)
                    {
                        this.RunTaskCount++;
                        this.Task.OnDynamicInvoke();
                        this.TotalRuntime += this.Task.RunTime;

                        if (this.Task.IsFaulted &&
                            this.Task.InnerException != null &&
                            ThreadException != null)
                        {
                            ThreadException(this, new ThreadExceptionEventArgs(this.Task.InnerException));
                        }
                    }

                    WorkTask task = null;
                    if (this.TaskScheduler.TryDispatchWorkTask(out task))
                    {
                        if (task.Status == TaskStatus.WaitingForActivation)
                        {
                            task.Status = TaskStatus.WaitingToRun;
                        }
                        this.Task = task;
                    }
                    else
                    {
                        reset.Reset();
                        m_IsWaiting = true;
                        if (!reset.WaitOne(60000))
                        {
                            break;
                        }
                    }
                } while (true);
            }
            catch (System.Exception ex)
            {
                if (ThreadException != null)
                {
                    ThreadException(this, new ThreadExceptionEventArgs(ex));
                }
            }
        }
        public static WorkTask CreateTask <T1, TResult>(Func <T1, TResult> method, params object[] args)
        {
            if (args == null || args.Length != 1)
            {
                throw new ArgumentOutOfRangeException();
            }

            WorkTask task = new WorkTask(method, args);

            task.DynamicInvoke = (delegate()
            {
                task.ReturnValue = method((T1)task.MethodArgs[0]);
            });
            return(task);
        }
 /// <summary>
 /// 将 WorkTask 排队到计划程序中
 /// </summary>
 /// <param name="task"></param>
 protected internal virtual void QueueTask(WorkTask task)
 {
     if (task.ExecuteOnlyOnce || task.PlanStartTime.Ticks <= DateTime.Now.Ticks)
     {
         if (!TryExecuteTask(task))
         {
             g_QueueTasks.Enqueue(task);
         }
     }
     else
     {
         lock (m_lstTasks)
             m_lstTasks.Add(task);
     }
 }
        public static WorkTask CreateTask <T1, T2>(Action <T1, T2> method, params object[] args)
        {
            if (args == null || args.Length != 2)
            {
                throw new ArgumentOutOfRangeException();
            }

            WorkTask task = new WorkTask(method, args);

            task.DynamicInvoke = (delegate()
            {
                method((T1)task.MethodArgs[0]
                       , (T2)task.MethodArgs[1]);
            });
            return(task);
        }
        protected virtual void OnDispatchingTask()
        {
            int iCount = 0;

            while (this.IsRunning)
            {
                try
                {
                    WorkTask task = null;
                    for (int i = 0; i < m_lstTasks.Count; i++)
                    {
                        task = m_lstTasks[i];
                        if (task == null)
                        {
                            lock (m_lstTasks)
                                m_lstTasks.RemoveAt(i);
                            continue;
                        }
                        if (task.ExecuteOnlyOnce || task.PlanStartTime.Ticks <= DateTime.Now.Ticks)
                        {
                            g_QueueTasks.Enqueue(task);
                            lock (m_lstTasks)
                                m_lstTasks.RemoveAt(i);
                        }
                    }

                    TryExecuteTask();
                }
                catch (Exception ex)
                {
                    m_lstException.Add(ex);
                }
                iCount++;
                if (iCount % 100 == 0 && m_lstException.Count > 0)
                {
                    AggregateException lst = null;
                    lock (m_lstException)
                    {
                        lst = new AggregateException(m_lstException);
                        m_lstException.Clear();
                    }
                    WorkTask task = WorkTask.CreateTask <AggregateException>(OnDispatchException, lst);
                    task.Start(this);
                }
                Thread.Sleep(1);
            }
        }
 /// <summary>
 /// 开始新任务
 /// </summary>
 /// <param name="task"></param>
 /// <returns>线程挂起、结束时,返回true, 否则失败</returns>
 public bool Start(WorkTask task)
 {
     lock (lockObject)
     {
         if (task == null || !this.IsIdle)
         {
             return(false);
         }
         m_IsWaiting = false;
         task.Status = TaskStatus.WaitingToRun;
         this.Task   = task;
         reset.Set();
         if (!this.IsRunnig)
         {
             m_Thread              = new Thread(new ThreadStart(Working));
             m_Thread.Name         = "ThreadPool";
             m_Thread.IsBackground = true;
             m_Thread.Start();
         }
         return(true);
     }
 }
 protected internal bool TryDispatchWorkTask(out WorkTask task)
 {
     return(g_QueueTasks.TryDequeue(out task));
 }