public UpdateJobs GetJobs() { UpdateJobs myJobs = new UpdateJobs(); IEnumBackgroundCopyJobs copyJobs; IBackgroundCopyJob retrievedJob; UInt32 uintFetched = Convert.ToUInt32(0); Int32 intFetched; try { copyManager.EnumJobs(Convert.ToUInt32(0), out copyJobs); do { copyJobs.Next(Convert.ToUInt32(1), out retrievedJob, ref uintFetched); intFetched = Convert.ToInt32(uintFetched); if (intFetched == 1) { myJobs.Add(new UpdateJob(retrievedJob)); } }while (intFetched == 1); return(myJobs); } catch (COMException ex) { throw new Exception(String.Format("Error Creating Job ({0}).", ex.Message)); } }
public static Dictionary <Guid, BitsJob> GetAllJobs() { uint dwFlags = 0; // 0 - to get all jobs IEnumBackgroundCopyJobs bcmJobs; _bcm.EnumJobs(dwFlags, out bcmJobs); uint jobsCount; bcmJobs.GetCount(out jobsCount); _jobs.Clear(); for (int i = 0; i < jobsCount; i++) { IBackgroundCopyJob job; uint jobsFetched; bcmJobs.Next(1, out job, out jobsFetched); if (jobsFetched != 1) { throw new Exception("improper number of jobs fetched"); } Guid jobID; job.GetId(out jobID); _jobs.Add(jobID, new BitsJob(job)); } return(_jobs); }
/// <summary> /// Gets all the jobs currently being managed with the system. /// </summary> public IEnumerable <IDownloadJob> GetAll() { var jobs = new List <DownloadJob> (); IBackgroundCopyManager bitsManager = null; IEnumBackgroundCopyJobs enumJobs = null; try { bitsManager = (IBackgroundCopyManager) new BackgroundCopyManager(); bitsManager.EnumJobs(0, out enumJobs); uint fetched; IBackgroundCopyJob bitsJob = null; try { enumJobs.Next(1, out bitsJob, out fetched); while (fetched == 1) { Guid id; bitsJob.GetId(out id); jobs.Add(new DownloadJob(id, bitsJob)); enumJobs.Next(1, out bitsJob, out fetched); } } finally { if (bitsJob != null) { Marshal.ReleaseComObject(bitsJob); } } return(jobs.ToArray()); } finally { if (enumJobs != null) { Marshal.ReleaseComObject(enumJobs); } if (bitsManager != null) { Marshal.ReleaseComObject(bitsManager); } } }
/// <summary> /// used by externally visible overload. /// </summary> /// <param name="isDisposing">whether or not to clean up managed + unmanaged/large (true) or just unmanaged(false)</param> private void Dispose(bool isDisposing) { const uint BG_JOB_ENUM_CURRENT_USER = 0; // const uint BG_JOB_ENUM_ALL_USERS = 0x0001; leads to ACCESS DENIED errors IBackgroundCopyManager mgr = null; IEnumBackgroundCopyJobs jobs = null; IBackgroundCopyJob job = null; if (isDisposing) { try { mgr = (IBackgroundCopyManager)(new BackgroundCopyManager()); mgr.EnumJobs(BG_JOB_ENUM_CURRENT_USER, out jobs); uint numJobs; jobs.GetCount(out numJobs); // lock the jobs collection for duration of this operation lock (bitsDownloaderJobs.SyncRoot) { for (int i = 0; i < numJobs; i++) { // use jobs interface to walk through getting each job uint fetched; jobs.Next(1, out job, out fetched); // get jobid guid Guid jobID; job.GetId(out jobID); // check if the job is in OUR collection; if so cancel it. we obviously don't want to get // jobs from other Updater threads/processes, or other BITS jobs on the machine! if (bitsDownloaderJobs.Contains(jobID)) { // take ownership just in case, and cancel() it job.TakeOwnership(); job.Cancel(); // remove from our collection bitsDownloaderJobs.Remove(jobID); } } } } finally { if (null != mgr) { Marshal.ReleaseComObject(mgr); mgr = null; } if (null != jobs) { Marshal.ReleaseComObject(jobs); jobs = null; } if (null != job) { Marshal.ReleaseComObject(job); job = null; } } } }