Example #1
0
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="task">任务信息</param>
        /// <returns>任务添加是否成功</returns>
        internal bool add(taskInfo task)
        {
            bool isTask = false;

            Monitor.Enter(taskLock);
            try
            {
                if (!isStop)
                {
                    if (threadCount != maxThreadCount && freeThreadCount == 0)
                    {
                        ++threadCount;
                        try
                        {
                            if (maxThreadCount == 1)
                            {
                                threadPool.Start(runTask, Dispose);
                            }
                            else if (threadCount != 1)
                            {
                                threadPool.Start(runManyTask);
                            }
                            else
                            {
                                threadPool.Start(runManyTask, Dispose);
                            }
                        }
                        catch (Exception error)
                        {
                            log.Default.Add(error, null, true);
                            Monitor.Enter(waitFreeLock);
                            try
                            {
                                Monitor.Enter(waitLock);
                                try
                                {
                                    if (--threadCount == finallyThreadCount)
                                    {
                                        Monitor.PulseAll(waitLock);
                                    }
                                }
                                finally { Monitor.Exit(waitLock); }
                                if (threadCount == freeThreadCount)
                                {
                                    Monitor.PulseAll(waitFreeLock);
                                }
                            }
                            finally { Monitor.Exit(waitFreeLock); }
                        }
                    }
                    tasks.Add(task);
                    Monitor.Pulse(taskLock);
                    isTask = true;
                }
            }
            finally { Monitor.Exit(taskLock); }
            return(isTask);
        }
Example #2
0
 /// <summary>
 /// 添加新任务
 /// </summary>
 /// <param name="task">任务信息</param>
 private void add(taskInfo task)
 {
     if (task.RunTime != DateTime.MaxValue)
     {
         DateTime now = DateTime.Now;
         if (now < task.RunTime)
         {
             Monitor.Enter(taskLock);
             try
             {
                 if (task.RunTime < nearTime)
                 {
                     timer.Stop();
                     if (nearTime == DateTime.MaxValue)
                     {
                         tasks.Add(task);
                     }
                     else
                     {
                         tasks.AddExpand(task);
                     }
                     nearTime       = task.RunTime;
                     timer.Interval = (task.RunTime - now).TotalMilliseconds;
                     timer.Start();
                 }
                 else if (newTasks.Count == 0 && task.RunTime >= tasks[tasks.Count - 1].RunTime)
                 {
                     tasks.Add(task);
                 }
                 else
                 {
                     newTasks.Add(task);
                 }
             }
             finally { Monitor.Exit(taskLock); }
         }
         else
         {
             this.task.add(task.Info);
         }
     }
 }
Example #3
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="task">任务信息</param>
 /// <returns>任务添加是否成功</returns>
 internal bool Add(taskInfo task)
 {
     Monitor.Enter(taskLock);
     if (isStop == 0)
     {
         int threadCount = this.threadCount;
         try
         {
             pushTasks.Add(task);
             if (this.threadCount == 0)
             {
                 this.threadCount = 1;
                 freeWaitHandle.Reset();
             }
         }
         finally { Monitor.Exit(taskLock); }
         if (threadCount == 0)
         {
             try
             {
                 threadPool.FastStart(this, thread.callType.TaskQueueRun);
                 return(true);
             }
             catch (Exception error)
             {
                 Monitor.Enter(taskLock);
                 this.threadCount = 0;
                 Monitor.Exit(taskLock);
                 log.Error.Add(error, null, false);
             }
         }
         else
         {
             return(true);
         }
     }
     else
     {
         Monitor.Exit(taskLock);
     }
     return(false);
 }
Example #4
0
 /// <summary>
 /// 执行任务
 /// </summary>
 internal void Run()
 {
     do
     {
         Monitor.Enter(taskLock);
         if (pushTasks.Count == 0)
         {
             int threadCount = --this.threadCount;
             Monitor.Exit(taskLock);
             if (threadCount == 0)
             {
                 freeWaitHandle.Set();
             }
             break;
         }
         taskInfo task = pushTasks.UnsafePopExpandReset();
         Monitor.Exit(taskLock);
         task.Run();
     }while (true);
 }
Example #5
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="task">任务信息</param>
 /// <returns>任务添加是否成功</returns>
 internal bool Add(taskInfo task)
 {
     Monitor.Enter(taskLock);
     if (isStop == 0)
     {
         try
         {
             pushTasks.Add(task);
             if (this.threadCount == MaxThreadCount)
             {
                 return(true);
             }
             if (this.threadCount++ == 0)
             {
                 freeWaitHandle.Reset();
             }
         }
         finally { Monitor.Exit(taskLock); }
         try
         {
             threadPool.FastStart(this, thread.callType.TaskRun);
         }
         catch (Exception error)
         {
             Monitor.Enter(taskLock);
             int count = --this.threadCount | pushTasks.Count;
             Monitor.Exit(taskLock);
             if (count == 0)
             {
                 freeWaitHandle.Set();
             }
             log.Error.Add(error, null, false);
         }
         return(true);
     }
     else
     {
         Monitor.Exit(taskLock);
     }
     return(false);
 }
Example #6
0
        /// <summary>
        /// 触发定时任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void onTimer(object sender, ElapsedEventArgs e)
        {
            DateTime        now  = DateTime.Now;
            list <taskInfo> runs = null;

            Monitor.Enter(taskLock);
            try
            {
                timer.Stop();
                int count = 0;
                foreach (taskInfo task in tasks)
                {
                    if (task.RunTime <= now)
                    {
                        ++count;
                    }
                    else
                    {
                        break;
                    }
                }
                runs = new list <taskInfo>(tasks.GetSub(0, count), true);
                tasks.Sub(count, tasks.Count - count);
                if (newTasks.Count != 0)
                {
                    taskInfo[] newArray = newTasks.Unsafer.Array;
                    int        left     = 0;
                    while (left != newTasks.Count && newArray[left].RunTime > now)
                    {
                        ++left;
                    }
                    if (left != newTasks.Count)
                    {
                        int right = newTasks.Count;
                        while (right != 0 && newArray[--right].RunTime <= now)
                        {
                            ;
                        }
                        if (left < right)
                        {
                            do
                            {
                                taskInfo task = newArray[left];
                                newArray[left]  = newArray[right];
                                newArray[right] = task;
                                while (newArray[++left].RunTime > now)
                                {
                                    ;
                                }
                                while (newArray[--right].RunTime <= now)
                                {
                                    ;
                                }
                            }while (left < right);
                        }
                        runs.Add(newArray, left, newTasks.Count - left);
                        newTasks.Unsafer.AddLength(left - newTasks.Count);
                    }
                    if (newTasks.Count != 0)
                    {
                        newArray = newTasks.sort(value => value.RunTime);
                        if (tasks.Count == 0)
                        {
                            tasks = new collection <taskInfo>(newArray, true);
                        }
                        else
                        {
                            tasks = new collection <taskInfo>(unsafer.array.GetMergeSort(tasks.ToArray(), newArray, value => value.RunTime), true);
                        }
                        newTasks.Empty();
                    }
                }
                if (tasks.Count != 0)
                {
                    taskInfo task = tasks[0];
                    nearTime       = task.RunTime;
                    timer.Interval = (task.RunTime - now).TotalMilliseconds;
                    timer.Start();
                }
                else
                {
                    nearTime = DateTime.MaxValue;
                }
            }
            catch (Exception error)
            {
                fastCSharp.log.Default.Add(error.ToString());
                timer.Interval = 1;
                timer.Start();
            }
            finally
            {
                Monitor.Exit(taskLock);
                foreach (taskInfo task in runs)
                {
                    task.Info.Run();
                }
            }
        }