コード例 #1
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);
                }
            });
        }