Пример #1
0
        public TreeTask(TreeTaskInfo taskInfo)
        {
            this.Info               = taskInfo;
            this._threadCounter     = new TreeTaskThreadCounter(taskInfo.Connection);
            this._waitingJobs       = new ConcurrentQueue <TreeJob>();
            this._runningJobs       = new ObservableCollection <TreeJob>();
            this._runningJobsLock   = new object();
            this.CancellationSource = new CancellationTokenSource();
            this.Progress           = new TreeTaskProgressInfo();

            Progress.AddItems(1);
        }
Пример #2
0
        private void Execute(TreeJob treeJob)
        {
            lock (this._runningJobsLock)
            {
                this._threadCounter.Increment();

                treeJob.State = TreeJobState.Running;
                this._runningJobs.Add(treeJob);
                OnJobChanged(treeJob, TreeJobChangedEvent.RunningAdded);
            }

            Task.Factory.StartNew(treeJob.Action)
            .ContinueWith(t =>
            {
                lock (this._runningJobsLock)
                {
                    this._runningJobs.Remove(treeJob);
                }

                treeJob.State = TreeJobState.Completed;
                OnJobCompleted(treeJob);

                Progress.AddItems(treeJob.PromisedChildCount);

                Progress.CompleteItems(1);
                this._threadCounter.Decrement();

                OnJobChanged(treeJob, TreeJobChangedEvent.RunningCompleted);

                CheckOnCompleted();
            })
            .ContinueOnException(t =>
            {
                if (t.Exception != null)
                {
                    log.ErrorFormat("Error occurred while processing task completion: {0}", t.Exception);
                }
            });
        }