Esempio n. 1
0
        public BitsManager()
        {
            // Set threading apartment
            System.Threading.Thread.CurrentThread.TrySetApartmentState(ApartmentState.STA);
            int hResult = NativeMethods.CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, RpcAuthnLevel.Connect, RpcImpLevel.Impersonate, IntPtr.Zero, EoAuthnCap.None, IntPtr.Zero);

            this.manager = new BackgroundCopyManager() as IBackgroundCopyManager;
            this.jobs = new BitsJobs(this); // will be set correctly later after initialization
            this.notificationHandler = new BitsNotification(this);
            this.notificationHandler.OnJobErrorEvent += new EventHandler<ErrorNotificationEventArgs>(notificationHandler_OnJobErrorEvent);
            this.notificationHandler.OnJobModifiedEvent += new EventHandler<NotificationEventArgs>(notificationHandler_OnJobModifiedEvent);
            this.notificationHandler.OnJobTransferredEvent += new EventHandler<NotificationEventArgs>(notificationHandler_OnJobTransferredEvent);
        }
Esempio n. 2
0
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    foreach (BitsJob job in this.Jobs.Values)
                    {
                        job.Dispose();
                    }

                    this.jobs.Clear();
                    this.jobs.Dispose();
                    Marshal.ReleaseComObject(manager);
                    manager = null;
                }
            }
            disposed = true;
        }
Esempio n. 3
0
        /// <summary>Releases unmanaged and - optionally - managed resources.</summary>
        /// <param name="disposing"><c>True</c> to release both managed and unmanaged resources; otherwise, <c>False</c> to release only unmanaged resources.</param>
        void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    foreach (var job in this.Jobs.Values)
                    {
                        job.Dispose();
                    }

                    this.Jobs.Clear();
                    this.Jobs.Dispose();
                    Marshal.ReleaseComObject(this.BackgroundCopyManager);
                    this.BackgroundCopyManager = null;
                }
            }

            this.disposed = true;
        }
        /// <summary>
        /// Internal copy-job factory method.  Used to coordinate all aspects of a job set-up, 
        /// which includes creating a copy manager, creating a job within it, setting download
        /// parameters, and adding the job to our tracking collection for cleanup later
        /// </summary>
        /// <param name="copyManager">null reference to copy manager</param>
        /// <param name="copyJob">null reference to copy job</param>
        /// <param name="jobID">null reference to job id guid</param>
        /// <param name="jobNameKey">the key used to look up the job name in the resource file</param>
        /// <param name="jobDescriptionKey">the key used to look up the job description in the resource file</param>
        private void CreateCopyJob( 
            out IBackgroundCopyManager copyManager,
            out IBackgroundCopyJob copyJob,
            ref Guid jobID,
            string jobNameKey,
            string jobDescriptionKey)
        {
            string jobName = Resource.ResourceManager[ jobNameKey ];
            string jobDesc = Resource.ResourceManager[ jobDescriptionKey ];

            //  wrap in try-finally so we can clean COM objects if unexpected error
            try
            {
                //  create the manager
                copyManager = (IBackgroundCopyManager) new BackgroundCopyManager();

                // create the job, set its description, use "out" semantics to get jobid and the actual job object
                copyManager.CreateJob(
                    jobName,
                    BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD,
                    out jobID,
                    out copyJob );

                //  set useful description
                copyJob.SetDescription( jobDesc );

                //  ***
                //      SET UP BITS JOB SETTINGS--TIMEOUTS/RETRY ETC
                //      SEE THE FOLLOWING REFERENCES:
                //  **  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/bits/bits/ibackgroundcopyjob_setminimumretrydelay.asp?frame=true
                //  **  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/bits/bits/ibackgroundcopyjob_setnoprogresstimeout.asp?frame=true
                //  **  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/bits/bits/bg_job_priority.asp
                //  ***

                //  in constant set to 0; this makes BITS retry as soon as possible after an error
                copyJob.SetMinimumRetryDelay( (uint)BITS_SET_MINIMUM_RETRY_DELAY );
                //  in constant set to 5 seconds; BITS will set job to Error status if exceeded
                copyJob.SetNoProgressTimeout( (uint)BITS_SET_NO_PROGRESS_TIMEOUT );
                //  make this job the highest (but still background) priority--
                copyJob.SetPriority( BG_JOB_PRIORITY.BG_JOB_PRIORITY_HIGH );
                //  ***

                //  lock our internal collection of jobs, and add this job--we use this collection in Dispose()
                //  to tell BITS to Cancel() jobs--and remove them from the queue
                //  if we did not do this, BITS would continue for (by default) two weeks to download what we asked!
                lock( _jobs.SyncRoot )
                {
                    _jobs.Add( jobID, jobName );
                }
            }
            catch( Exception e )
            {
                //  bad to catch all exceptions, but OK because we adorn it with necessary additional info then pass it up as innerException
                string error = ApplicationUpdateManager.TraceWrite( e, "[BITSDownloader.CreateCopyJob]", "RES_EXCEPTION_BITSOtherError", jobID, jobName, jobDesc );
                //  publish
                Exception newE = new Exception( error, e );
                ExceptionManager.Publish( newE );
                //  rethrow;
                throw newE;
            }
        }
 protected override void Dispose( bool disposing )
 {
     if ( !IsDisposed )
     {
         if ( _bitsManager != null )
         {
             Marshal.ReleaseComObject( _bitsManager );
             _bitsManager = null;
         }
     }
     base.Dispose( disposing );
 }