/// <summary>
        /// Callback. Used by the windows update api when the download completes. Do not call this method.
        /// </summary>
        void Invoke(IDownloadJob downloadJob, IDownloadCompletedCallbackArgs callbackArgs)
        {
            bool doCallback = false;

            lock (JobLock)
            {
                if (Job != null && Job.InternalJobObject == downloadJob && Job.IsCompleted)
                {
                    StopTimeoutTimer();
                    doCallback = true;
                }
            }
            // calling the callback inside the lock can lead to deadlocks when the callback tries to dispose this object
            if (doCallback)
            {
                _completedCallback(_uDownloader.EndDownload(DownloadJob), _uDownloader.Updates);
            }
        }
Beispiel #2
0
        void DownloadComplete()
        {
            _downloadResult = _updateDownloader.EndDownload(_downloadJob);

            _downloadJob = null;

            if (_downloadResult.ResultCode == OperationResultCode.orcSucceeded ||
                _downloadResult.ResultCode == OperationResultCode.orcSucceededWithErrors)
            {
                this.ShowProgressMessage("progress_download", "");

                if (_downloadResult.ResultCode == OperationResultCode.orcSucceeded)
                {
                    this.AppendString("下载完成。\r\n");
                }
                else
                {
                    this.AppendString("下载部分完成,部分出错。\r\n");
                }

#if NO
                DialogResult result = MessageBox.Show(this,
                                                      "要安装这些更新么?",
                                                      "WindowsUpdateDialog",
                                                      MessageBoxButtons.YesNo,
                                                      MessageBoxIcon.Question,
                                                      MessageBoxDefaultButton.Button2);
                if (result == System.Windows.Forms.DialogResult.No)
                {
                    OnAllComplete();
                    return;
                }
#endif

                BeginInstallation();
            }
            else
            {
                this.AppendString("下载更新失败。错误码: " + _downloadResult.ResultCode);
                // 全部结束
                OnAllComplete();
            }
        }
Beispiel #3
0
        //Downlaod Complete callback
        void IDownloadCompletedCallback.Invoke(IDownloadJob downloadJob, IDownloadCompletedCallbackArgs callbackArgs)
        {
            try
            {
                var downloadResult = updateDownloader.EndDownload(downloadJob);

                //todo could downloads partially fail: the result above not success, but still some  updates available for install?
                if (downloadResult.ResultCode != OperationResultCode.orcSucceeded && downloadResult.ResultCode != OperationResultCode.orcSucceededWithErrors)
                {
                    DebugLog($"Download failed with code: {downloadResult.ResultCode}");
                    //return;
                    DownloadCompleted?.Invoke(false);
                }
                else
                {
                    //ReadyToInstall?.Invoke();
                    DownloadCompleted?.Invoke(true);
                }

                for (int i = 0; i < downloadJob.Updates.Count; i++)
                {
                    var resultCode = downloadResult.GetUpdateResult(i).ResultCode;
                    var title      = downloadJob.Updates[i].Title;
                    if (resultCode != OperationResultCode.orcSucceeded)
                    {
                        //the title must be there
                        //but if it it not for some reason we will detect this (via dict. exception)
                        updateResults[title].Error       = resultCode.ToString();
                        updateResults[title].IsInstalled = false;
                    }


                    DebugLog($"Download status for update {title}: {resultCode}");
                }
            }
            catch (Exception ex)
            {
                DownloadCompleted?.Invoke(false);

                Log.Error("IDownloadCompletedCallback.Invoke", ex.ToString());
            }
        }
        /// <summary>
        /// アップデートのダウンロードを非同期に行う
        /// </summary>
        /// <param name="downloader"></param>
        /// <param name="progress"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task <IDownloadResult> DownloadAsync(this IUpdateDownloader downloader, Action <IDownloadProgress> progress, CancellationToken cancellationToken = default(CancellationToken))
        {
            var task = new TaskCompletionSource <IDownloadResult>();

            if (cancellationToken.IsCancellationRequested)
            {
                task.TrySetCanceled(cancellationToken);
                return(task.Task);
            }
            var job = null as IDownloadJob;
            var reg = null as CancellationTokenRegistration?;

            job = downloader.BeginDownload(new DownloadProgressChangedCallback(progress),
                                           new DownloadCompletedCallback((_job2, args) =>
            {
                try
                {
                    try
                    {
                        task.TrySetResult(downloader.EndDownload(_job2));
                    }
                    catch (Exception e)
                    {
                        task.TrySetException(e);
                    }
                }
                finally
                {
                    job = null;
                    reg?.Dispose();
                }
            }), null);
            reg = cancellationToken.Register(() =>
            {
                task.TrySetCanceled(cancellationToken);
                job?.RequestAbort();
            });
            return(task.Task);
        }
Beispiel #5
0
        public void Start(bool showProgress)
        {
            this.ShowProgress = showProgress;

            Console.WriteLine("WUA_Starting");

            IUpdateSession updateSession = new UpdateSessionClass();

            IUpdateSearcher   updateSearcher   = updateSession.CreateUpdateSearcher();
            IUpdateDownloader updateDownloader = updateSession.CreateUpdateDownloader();
            IUpdateInstaller  updateInstaller  = updateSession.CreateUpdateInstaller();

            if (updateInstaller.IsBusy)
            {
                Console.WriteLine("WUA_IsBusy");
                return;
            }

            // SEARCHING

            Console.WriteLine("WUA_FindingUpdates");

            ISearchResult searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'");

            if (searchResult.Updates.Count.Equals(0))
            {
                Console.WriteLine("WUA_NoApplicableUpdates");
                return;
            }

            // LISTING

            UpdateCollection updateToDownload = new UpdateCollectionClass();

            for (int i = 0; i < searchResult.Updates.Count; i++)
            {
                IUpdate update = searchResult.Updates[i];

                Console.WriteLine("WUA_UpdateItem:{0}|{1}", i + 1, update.Title);

                if (!update.IsDownloaded)
                {
                    updateToDownload.Add(update);
                }
            }

            // DOWNLOADING

            if (!updateToDownload.Count.Equals(0))
            {
                Console.WriteLine("WUA_DownloadingStarted");

                updateDownloader.Updates  = updateToDownload;
                updateDownloader.IsForced = true;
                updateDownloader.Priority = DownloadPriority.dpHigh;

                IDownloadJob    job = updateDownloader.BeginDownload(this, this, updateDownloader);
                IDownloadResult downloaderResult = updateDownloader.EndDownload(job);

                Console.WriteLine("WUA_DownloadingCompleted:{0}", downloaderResult.ResultCode);
            }

            // INSTALLATION

            updateInstaller.Updates  = searchResult.Updates;
            updateInstaller.IsForced = true;

            Console.WriteLine("WUA_InstallationStarted");

            IInstallationJob    installationJob = updateInstaller.BeginInstall(this, this, updateInstaller);
            IInstallationResult result          = updateInstaller.EndInstall(installationJob);

            Console.WriteLine("WUA_InstallationCompleted:{0}|{1}", result.ResultCode, result.RebootRequired);

            // RESULT

            for (int i = 0; i < searchResult.Updates.Count; i++)
            {
                IUpdateInstallationResult resultItem = result.GetUpdateResult(i);
                Console.WriteLine("WUA_InstallationResult:{0}|{1}|{2}",
                                  i + 1, resultItem.ResultCode, resultItem.RebootRequired);
            }

            Console.WriteLine("WUA_Finish");
        }