private OperationResultCode InstallUpdatesUtil(CancellationToken cancellationToken)
        {
            try
            {
                TimeSpan         operationTimeOut = TimeSpan.FromMinutes(this.GetRemainingInstallationTimeout());
                UpdateCollection updatesToInstall = new UpdateCollection();
                foreach (WUUpdateWrapper item in this._wuCollectionWrapper.Collection.Values)
                {
                    if (item.IsDownloaded && !item.IsInstalled)
                    {
                        updatesToInstall.Add(item.Update);
                    }
                }
                // if no updates to install
                if (updatesToInstall.Count == 0)
                {
                    _eventSource.InfoMessage("No updates to install.");
                    return(OperationResultCode.orcSucceeded);
                }

                IUpdateInstaller uInstaller = this._uSession.CreateUpdateInstaller();
                uInstaller.Updates = updatesToInstall;

                InstallationCompletedCallback installationCompletedCallback = new InstallationCompletedCallback();
                IInstallationJob installationJob = uInstaller.BeginInstall(new InstallationProgressChangedCallback(),
                                                                           installationCompletedCallback, null);

                if (
                    !this._helper.WaitOnTask(installationCompletedCallback.Task,
                                             (int)operationTimeOut.TotalMilliseconds, cancellationToken))
                {
                    _eventSource.Message("installationJob : Requested Abort");
                    installationJob.RequestAbort();
                }

                IInstallationResult uResult = uInstaller.EndInstall(installationJob);
                for (int i = 0; i < updatesToInstall.Count; i++)
                {
                    var hResult  = uResult.GetUpdateResult(i).HResult;
                    var updateID = updatesToInstall[i].Identity.UpdateID;
                    this._wuCollectionWrapper.Collection[updateID].IsInstalled = (hResult == 0);
                    this._wuCollectionWrapper.Collection[updateID].HResult     = hResult;
                    if (hResult != 0)
                    {
                        _eventSource.WarningMessage(string.Format("Install for update ID {0} returned hResult {1}", updateID, hResult));
                    }
                }

                return(uResult.ResultCode);
            }
            catch (Exception e)
            {
                _eventSource.InfoMessage("Exception while installing Windows-Updates: {0}", e);
                return(OperationResultCode.orcFailed);
            }
        }
        public async Task <IInstallationResult> InstallAsync(UpdateCollection updates, CancellationToken cancellationToken)
        {
            var installer = this.session.CreateUpdateInstaller();

            installer.Updates = updates;

            var completedCallback = new InstallationCompletedCallback();
            var progressCallback  = new InstallationProgressChangedCallback();

            var job = installer.BeginInstall(progressCallback, completedCallback, null);

            if (await completedCallback.WaitAsync() == false)
            {
                job.RequestAbort();
            }

            if (cancellationToken.IsCancellationRequested)
            {
                job.RequestAbort();
            }

            return(installer.EndInstall(job));
        }