예제 #1
0
 /// <summary>
 /// 任务完成
 /// </summary>
 protected void Complete(TaskCompletedReason reason)
 {
     try
     {
         this.Log.Info("任务执行完成。Task Id:" + this.ID);
         this.OnComplete(reason);
         if (Completed != null)
         {
             this.Completed(this, null);
         }
     }
     catch (Exception ex)
     {
         this.Log.Error("任务执行完成是失败。Task ID:" + this.ID, ex);
         this.OnExcepton(this.PrimaryData, default(I), ex);
     }
 }
예제 #2
0
 /// <summary>
 /// 任务完成
 /// </summary>
 protected virtual void OnComplete(TaskCompletedReason reason)
 {
 }
예제 #3
0
        /// <summary>
        /// 执行进程
        /// </summary>
        private void ExecuteThread(object nothing)
        {
            this.PreExecute();
            int exCount = this.Status.Crculative ? 0 : this.ErrorMaximum;//是否只处理一次

            while (exCount <= this.ErrorMaximum && !this.Status.Suspended)
            {
                Thread.Sleep(100);                  //避免一直占用cpu
                I         currentItem = default(I); //当前正在处理的数据
                IList <I> datas       = null;
                bool      hasError    = false;
                try
                {
                    this.Log.Info("准备数据。Task Id:" + this.ID);
                    datas = this.PrepareData();
                }
                catch (Exception ex)
                {
                    this.Log.Error("获取数据失败。Task Id:" + this.ID, ex);
                    this.OnExcepton(this.PrimaryData, currentItem, ex);
                    exCount++;
                    hasError = true;
                }
                if (hasError)
                {
                    continue;
                }
                if (datas == null || datas.Count == 0)
                {
                    break;//没有数据则结束处理
                }
                foreach (I item in datas)
                {
                    try
                    {
                        currentItem = item;
                        if (this.Status.Suspended)
                        {
                            break;//是否已经中止处理?
                        }
                        this.Status.LastActiveTime = DateTime.UtcNow;
                        this.Preoperation(item);
                        this.Operating(item);
                        this.Operated(item);
                    }
                    catch (Exception ex)
                    {
                        this.Log.Error("处理数据失败。Task ID:" + this.ID, ex);
                        try
                        {
                            this.OnExcepton(this.PrimaryData, currentItem, ex);
                        }
                        catch (Exception ex1)
                        {
                            this.Log.Error("记录异常日志出错。Task ID:" + this.ID, ex1);
                        }
                        exCount++;
                    }
                }
            }
            TaskCompletedReason reason = TaskCompletedReason.Normal;

            if (exCount > this.ErrorMaximum)
            {
                reason = TaskCompletedReason.OutErrorMaximum;
            }
            else if (this.Status.Suspended)
            {
                reason = TaskCompletedReason.Suspended;
            }
            this.Complete(reason);
        }