public void TakeOwnership() { //Converts ownership to the current user copyJob.TakeOwnership(); }
/// <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; } } } }