private void CheckIfThrottlingJobIsComplete()
            {
                if (_throttlingJob.IsThrottlingJobCompleted)
                {
                    List <PSDataCollection <PSStreamObject> > resultsToAggregate = new List <PSDataCollection <PSStreamObject> >();
                    lock (_myLock)
                    {
                        foreach (Job registeredJob in _monitoredJobs)
                        {
                            resultsToAggregate.Add(registeredJob.Results);
                        }

                        foreach (Job throttledJob in _throttlingJob.GetChildJobsSnapshot())
                        {
                            resultsToAggregate.Add(throttledJob.Results);
                        }

                        resultsToAggregate.Add(_throttlingJob.Results);
                    }

                    foreach (PSDataCollection <PSStreamObject> resultToAggregate in resultsToAggregate)
                    {
                        this.AggregateJobResults(resultToAggregate);
                    }

                    lock (_myLock)
                    {
                        if (!_disposed && !_aggregatedResults.IsAddingCompleted)
                        {
                            _aggregatedResults.CompleteAdding();
                        }
                    }
                }
            }
            public static void ForwardAllResultsToCmdlet(ThrottlingJob throttlingJob, Cmdlet cmdlet, CancellationToken?cancellationToken)
            {
                using (var helper = new ForwardingHelper(throttlingJob))
                {
                    try
                    {
                        throttlingJob.ChildJobAdded += helper.ThrottlingJob_ChildJobAdded;

                        try
                        {
                            throttlingJob.StateChanged += helper.ThrottlingJob_StateChanged;

                            IDisposable cancellationTokenRegistration = null;
                            if (cancellationToken.HasValue)
                            {
                                cancellationTokenRegistration = cancellationToken.Value.Register(helper.CancelForwarding);
                            }

                            try
                            {
                                Interlocked.MemoryBarrier();
                                ThreadPool.QueueUserWorkItem(
                                    delegate
                                {
                                    helper.StartMonitoringJob(throttlingJob);
                                    foreach (Job childJob in throttlingJob.GetChildJobsSnapshot())
                                    {
                                        helper.StartMonitoringJob(childJob);
                                    }

                                    helper.CheckIfThrottlingJobIsComplete();
                                });

                                helper.ForwardResults(cmdlet);
                            }
                            finally
                            {
                                if (cancellationTokenRegistration != null)
                                {
                                    cancellationTokenRegistration.Dispose();
                                }
                            }
                        }
                        finally
                        {
                            throttlingJob.StateChanged -= helper.ThrottlingJob_StateChanged;
                        }
                    }
                    finally
                    {
                        throttlingJob.ChildJobAdded -= helper.ThrottlingJob_ChildJobAdded;
                    }
                }
            }