/// <summary>Moves the enumerator index to the next object in the collection.</summary>
 /// <returns></returns>
 public bool MoveNext()
 {
     try
     {
         icurrentjob = ienum.Next(1)?.FirstOrDefault();
         return(icurrentjob != null);
     }
     catch { return(false); }
 }
            /// <summary>
            /// Moves the enumerator index to the next object in the collection.
            /// </summary>
            /// <returns></returns>
            public bool MoveNext()
            {
                uint cnt = 0;

                try
                {
                    ienum.Next(1, out icurrentjob, ref cnt);
                }
                catch { }
                return(cnt == 1);
            }
        /// <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);
                }
            }
        }
예제 #4
0
 public MsBitsJobs(IEnumBackgroundCopyJobs jobs)
     : base(() => jobs.Reset(),
            () =>
 {
     IBackgroundCopyJob job = null;
     uint fetched           = 0;
     jobs.Next(1, out job, out fetched);
     return(fetched == 1 ? job : null);
 },
            () =>
 {
     uint count;
     jobs.GetCount(out count);
     return((int)count);
 })
 {
 }
예제 #5
0
        /// <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;
                    }
                }
            }
        }