private void ctxmiResume_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 ((bitsJob.State != JobState.Acknowledged) && (bitsJob.State != JobState.Canceled)) { bitsJob.Resume(); } this.ControlsFromJobState(); } }
private static void ManageJobState(string button, BitsJob job) { switch (button) { case "Resume": job.Resume(); break; case "Cancel": job.Cancel(); break; } }
/// <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)); } } }