示例#1
0
        /// <summary>
        /// Monitor the download status at a given frequency
        /// </summary>
        /// <param name="frequencySeconds">The frequency i9n seconds</param>
        private void MonitorDownLoad(double frequencySeconds = 1.0)
        {
            this.UpdateState = InstallAbility.Downloading;
            this.OnPropertyChanged("UpdateState");

            if (this.tokenSource != null)
            {
                this.tokenSource.Cancel();
                this.tokenSource.Dispose();
            }

            this.tokenSource = new CancellationTokenSource();

            TimeSpan frequency         = TimeSpan.FromSeconds(frequencySeconds);
            var      cancellationToken = this.tokenSource.Token;

            Task.Run(
                async() =>
            {
                while ((this.UpdateState = this.downloadAndInstallService.GetDownloadState()) == InstallAbility.Downloading)
                {
                    await Task.Delay(frequency);
                    this.OnPropertyChanged("DownloadProgress");
                }

                this.OnPropertyChanged("UpdateState");

                if (this.UpdateState == InstallAbility.ReadyToInstall)
                {
                    var response = this.downloadAndInstallService.InstallUpdate();
                    this.InstallCompleted?.Invoke(this, response);
                }
            },
                cancellationToken);
        }
        /// <summary>
        /// Get the status of the download
        /// </summary>
        /// <returns>The download state</returns>
        public InstallAbility GetDownloadState()
        {
            InstallAbility installAbility = InstallAbility.UpdateNotFound;

            if (this.HasDownloadReference)
            {
                var query = new DownloadManager.Query();
                query.SetFilterById(this.downloadReference);

                var cursor = this.downloadManager.InvokeQuery(query);
                if (cursor.MoveToFirst())
                {
                    var status = (DownloadStatus)cursor.GetInt(cursor.GetColumnIndex(DownloadManager.ColumnStatus));

                    switch (status)
                    {
                    case DownloadStatus.Successful:
                        installAbility = InstallAbility.ReadyToInstall;
                        break;

                    case DownloadStatus.Paused:
                    case DownloadStatus.Pending:
                    case DownloadStatus.Running:
                        installAbility = InstallAbility.Downloading;
                        break;

                    case DownloadStatus.Failed:
                        var reasonCode = (DownloadError)cursor.GetInt(cursor.GetColumnIndex(DownloadManager.ColumnReason));
                        if (reasonCode == DownloadError.InsufficientSpace || reasonCode == DownloadError.FileError || reasonCode == DownloadError.DeviceNotFound)
                        {
                            installAbility = InstallAbility.UnableToStoreOnDevice;
                        }
                        else if (reasonCode == DownloadError.FileAlreadyExists)
                        {
                            installAbility = InstallAbility.ReadyToInstall;
                        }
                        else if ((int)reasonCode == 404)
                        {
                            installAbility = InstallAbility.UpdateNotFound;
                        }
                        else
                        {
                            installAbility = InstallAbility.DownloadFailed;
                        }

                        break;
                    }
                }
            }

            return(installAbility);
        }