private OperationResultCode DownloadUpdatesUtil(CancellationToken cancellationToken)
        {
            try
            {
                UpdateDownloader uDownloader       = this._uSession.CreateUpdateDownloader();
                UpdateCollection updatesToDownload = new UpdateCollection();

                foreach (WUUpdateWrapper item in this._wuCollectionWrapper.Collection.Values)
                {
                    if (!item.IsDownloaded)
                    {
                        if (item.Update.EulaAccepted == false)
                        {
                            if (this._serviceSettings.AcceptWindowsUpdateEula)
                            {
                                try
                                {
                                    item.Update.AcceptEula();
                                }
                                catch (Exception e)
                                {
                                    _eventSource.WarningMessage(string.Format("Error occurred while accepting Eula for {0} . Exception : {1}",
                                                                              item, e));
                                }
                                updatesToDownload.Add(item.Update);
                            }
                        }
                        else
                        {
                            updatesToDownload.Add(item.Update);
                        }
                    }
                }

                uDownloader.Updates = updatesToDownload;

                DownloadCompletedCallback downloadCompletedCallback = new DownloadCompletedCallback();
                IDownloadJob downloadJob = uDownloader.BeginDownload(new DownloadProgressChangedCallback(),
                                                                     downloadCompletedCallback, null);

                TimeSpan operationTimeOut = TimeSpan.FromMinutes(this._serviceSettings.WUOperationTimeOutInMinutes);
                if (
                    !this._helper.WaitOnTask(downloadCompletedCallback.Task, (int)operationTimeOut.TotalMilliseconds,
                                             cancellationToken))
                {
                    _eventSource.Message("downloadJob : Requested Abort");
                    downloadJob.RequestAbort();
                }

                IDownloadResult uResult = uDownloader.EndDownload(downloadJob);
                for (int i = 0; i < updatesToDownload.Count; i++)
                {
                    var hResult  = uResult.GetUpdateResult(i).HResult;
                    var updateID = updatesToDownload[i].Identity.UpdateID;
                    this._wuCollectionWrapper.Collection[updateID].IsDownloaded = (hResult == 0);
                    if (hResult != 0)
                    {
                        _eventSource.WarningMessage(string.Format("Download for update ID {0} returned hResult {1}", updateID, hResult));
                    }
                }

                return(uResult.ResultCode);
            }
            catch (Exception e)
            {
                if ((uint)e.HResult == WUErrorCodes.WU_E_NO_UPDATE)
                {
                    return(OperationResultCode.orcSucceeded); // no updates found.
                }
                _eventSource.InfoMessage("Exception while downloading Windows-Updates: {0}", e);
                return(OperationResultCode.orcFailed);
            }
        }
Exemplo n.º 2
0
 void AbortDownload()
 {
     DebugLog("Requesting abort downlaod...");
     downloadJob_?.RequestAbort();
 }
Exemplo n.º 3
0
 public override void RequestAbort() => _job.RequestAbort();