コード例 #1
0
 public void DoPostLJobBatch(PostLastJobRunEventArgs arg)
 {
     if (PostJobBatch != null)
     {
         PostJobBatch(null, arg);
     }
 }
コード例 #2
0
        static public void StartProcessingJobBatch(JJobType jobType)
        {
            if (jobType.JobBatchObject != null)
            {
                throw new InvalidOperationException();                                //todo
            }
            BackgroundWorker mainWorker = new AbortableBackgroundWorker();

            mainWorker.WorkerSupportsCancellation = true;
            mainWorker.WorkerReportsProgress      = true;
            mainWorker.DoWork += (sd, ew) =>
            {
                BackgroundWorker wk = sd as BackgroundWorker;
                if (wk.CancellationPending)
                {
                    ew.Cancel = true;
                    return;
                }
                try
                {
                    if (jobType.JobBatchQueueSemaphore == null)
                    {
                        //first start
                        int maxTreadCount = 1;
                        if (JobConcurrentTypeEnum.Cancel.ToString().Equals(jobType.ConcurrentType))
                        {
                            maxTreadCount = 1;
                        }
                        else if (JobConcurrentTypeEnum.Wait.ToString().Equals(jobType.ConcurrentType))
                        {
                            maxTreadCount = 1;
                        }
                        else if (JobConcurrentTypeEnum.Allow.ToString().Equals(jobType.ConcurrentType))
                        {
                            maxTreadCount = jobType.MaxThreadCount;
                        }

                        jobType.JobBatchRunningDict.Clear();
                        jobType.JobBatchQueueSemaphore = new Semaphore(maxTreadCount, maxTreadCount);
                        jobType.JobBatchLog.Debug("JobType Semaphore created maxTreadCount: " + maxTreadCount);

                        jobType.JobBatchLastRunDate = DateTime.Now;
                        jobType.JobBatchStage       = RunningJobStageEnum.running.ToString();
                        Dm.Instance.SaveObject(jobType);
                    }

                    List <AutoResetEvent> endHandlers = new List <AutoResetEvent>();
                    int initialQueueCount             = jobType.JobBatchQueueCount;
                    int startedCount = 0;
                    while (true)
                    {
                        if (wk.CancellationPending)
                        {
                            ew.Cancel = true;
                            break;
                        }
                        if (jobType.JobBatchQueueCount == 0)
                        {
                            break;
                        }

                        jobType.JobBatchQueueSemaphore.WaitOne();

                        JRunningJob job = jobType.DequeueJob();
                        job.QueueSemaphore = jobType.JobBatchQueueSemaphore;
                        job.EndHandle      = new AutoResetEvent(false);
                        endHandlers.Add(job.EndHandle);

                        ScheduleJobBackgroundLocal(job);

                        startedCount++;
                        int progress = (int)(((double)startedCount / (double)initialQueueCount) * 100.0);
                        wk.ReportProgress(progress);
                    }
                    if (endHandlers.Count > 0)
                    {
                        WaitHandle.WaitAll(endHandlers.ToArray <AutoResetEvent>());
                    }

                    PostLastJobRunEventArgs arg1 = new PostLastJobRunEventArgs();
                    arg1.Canceled = ew.Cancel;
                    jobType.DoPostLJobBatch(arg1);
                    if (ew.Cancel == false)
                    {
                        ew.Result = RunningJobStageEnum.complated;
                    }
                }
                catch (Exception ex)
                {
                    jobType.JobBatchLog.Error("Job Batch fatal error", ex);
                    ew.Result = RunningJobStageEnum.exception;
                    JobManager.Instance.ComplateJobBatch(jobType, RunningJobStageEnum.exception);
                }
            };
            mainWorker.RunWorkerCompleted += (sd, ek) =>
            {
                ResetJobBatch(jobType);
                if (ek.Cancelled == true)
                {
                    JobManager.Instance.ComplateJobBatch(jobType, RunningJobStageEnum.aborted);
                }
                else
                {
                    if (RunningJobStageEnum.exception.Equals(ek.Result))
                    {
                        //do nothing
                    }
                    else if (RunningJobStageEnum.warning.Equals(ek.Result))
                    {
                        JobManager.Instance.ComplateJobBatch(jobType, RunningJobStageEnum.warning);
                    }
                    else if (RunningJobStageEnum.error.Equals(ek.Result))
                    {
                        JobManager.Instance.ComplateJobBatch(jobType, RunningJobStageEnum.error);
                    }
                    else
                    {
                        JobManager.Instance.ComplateJobBatch(jobType, RunningJobStageEnum.complated);
                    }
                }
            };
            mainWorker.ProgressChanged += (sd, pc) =>
            {
                localReportProgresJobType(jobType, pc.ProgressPercentage);
            };
            //run worker
            jobType.JobBatchObject = mainWorker;
            mainWorker.RunWorkerAsync();
        }
コード例 #3
0
 public void StandartPostLJobBatchEventHandler(object sender, PostLastJobRunEventArgs ew)
 {
     MessageBox.Show((ew.Canceled ? FrwUtilsRes.Job_batch_canceled : FrwUtilsRes.Job_batch_completed), FrwUtilsRes.Job_batch, MessageBoxButtons.OK, MessageBoxIcon.Information);
 }