Exemplo n.º 1
0
        private void RegisterEvents(BitsJob job)
        {
            job.OnJobError += (s, e) =>
            {
                _status.NextVersion = new Version();
                _status.BitsJobId   = Guid.Empty;
                _status.Save();
                OnUpdateDownloadError(new UpdateErrorEventArgs(e.Error));
                job.Cancel();
                job.Dispose();
            };

            job.OnJobTransferred += (s, e) =>
            {
                job.Complete();
                job.Dispose();
                OnUpdateDownloaded(new UpdateDownloadedEventArgs());
            };

            job.OnJobModified += (s, e) =>
            {
                if (job != null && job.State == JobState.Transferring)
                {
                    if (job.Progress != null)
                    {
                        OnUpdateDownloadProgressChanged(new UpdateProgressEventArgs(job.Progress.BytesTransferred, job.Progress.BytesTotal));
                    }
                }
            };
        }
Exemplo n.º 2
0
 private void ctxmiCancel_Click(object sender, EventArgs e)
 {
     if (this.lvJobList.SelectedItems.Count == 1)
     {
         JobWrapper tag     = this.lvJobList.SelectedItems[0].Tag as JobWrapper;
         BitsJob    bitsJob = tag.BitsJob;
         if (((MessageBox.Show(string.Format("Are you sure to delete job '{0}''", bitsJob.DisplayName), "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) && (bitsJob.State != JobState.Acknowledged)) && (bitsJob.State != JobState.Canceled))
         {
             bitsJob.Cancel();
         }
         this.ControlsFromJobState();
     }
 }
Exemplo n.º 3
0
        private static void ManageJobState(string button, BitsJob job)
        {
            switch (button)
            {
            case "Resume":
                job.Resume();
                break;

            case "Cancel":
                job.Cancel();
                break;
            }
        }
Exemplo n.º 4
0
        /// <summary>Downloads the updates using BITS.</summary>
        /// <param name="appUpdates">The application updates to download.</param>
        /// <param name="downloadName">The name of the job.</param>
        /// <param name="downloadLocation">The directory where the files are downloaded are stored.</param>
        /// <param name="isPriority">If set to <c>True</c> the updates will download with priority.</param>
        public static void DownloadUpdates(
            Collection <Sui> appUpdates, string downloadName, string downloadLocation, bool isPriority = false)
        {
            downloadDirectory = downloadLocation;
            jobName           = downloadName;
            if (appUpdates == null)
            {
                throw new ArgumentNullException(@"appUpdates");
            }

            if (appUpdates.Count < 1)
            {
                throw new ArgumentOutOfRangeException(@"appUpdates");
            }

            IsDownloading = true;

            // It's a new manager class
            manager = new BitsManager();

            // Assign the event handlers
            manager.OnJobTransferred -= ReportDownloadComplete;
            manager.OnJobError       -= ReportDownloadError;
            manager.OnJobModified    -= ReportDownloadProgress;
            manager.OnJobTransferred += ReportDownloadComplete;
            manager.OnJobError       += ReportDownloadError;
            manager.OnJobModified    += ReportDownloadProgress;

            // Load the BITS Jobs for the entire machine and current user
            manager.EnumJobs();
            manager.EnumJobs(JobOwner.AllUsers);

            int jobCount = 0;

            // Loops through the jobs, if a Seven Update job is found, try to resume, if not cancel it.
            foreach (var job in manager.Jobs.Values.Where(job => job.DisplayName == jobName).ToList())
            {
                jobCount++;
                job.EnumerateFiles();
                if (job.Files.Count < 1)
                {
                    job.Cancel();
                    jobCount--;
                }

                if (job.State == JobState.Suspended)
                {
                    job.Resume();
                }

                if (job.State == JobState.Transferred)
                {
                    job.Complete();
                    IsDownloading = false;
                    if (DownloadCompleted != null)
                    {
                        DownloadCompleted(null, new DownloadCompletedEventArgs(false));
                    }

                    return;
                }

                if ((job.State != JobState.Error && job.State != JobState.TransientError) && job.ErrorCount <= 0)
                {
                    continue;
                }

                Utilities.ReportError(
                    new WebException(job.Error.Description + " " + job.Error.ErrorCode), ErrorType.DownloadError);
                job.Cancel();
                jobCount--;
            }

            if (jobCount > 0)
            {
                return;
            }

            BitsJob bitsJob = manager.CreateJob(jobName, JobType.Download);

            bitsJob.Priority = isPriority ? JobPriority.Foreground : JobPriority.Normal;

            bitsJob.NotificationOptions = NotificationOptions.JobErrorOccurred | NotificationOptions.JobModified
                                          | NotificationOptions.JobTransferred;
            bitsJob.NoProgressTimeout = 60;
            bitsJob.MinimumRetryDelay = 60;
            foreach (var update in appUpdates)
            {
                DownloadUpdates(update, ref bitsJob);
            }

            bitsJob.EnumerateFiles();
            if (bitsJob.Files.Count > 0)
            {
                bitsJob.Resume();
            }
            else
            {
                bitsJob.Cancel();
                IsDownloading = false;
                if (DownloadCompleted != null)
                {
                    DownloadCompleted(null, new DownloadCompletedEventArgs(false));
                }
            }
        }