Ejemplo n.º 1
0
        /// <summary>
        /// Demonstrate minimal code for downloading a file using BITS.
        /// </summary>
        /// <param name="URL">Remote URL to download from; e.g. "https://aka.ms/WinServ16/StndPDF"</param>
        /// <param name="filename">Local file name to download to; e.g. @"C:\Server2016.pdf" </param>
        private BITS.IBackgroundCopyJob DownloadFile(string URL, string filename)
        {
            if (_mgr == null)
            {
                return(null);
            }

            BITS.GUID jobGuid;
            BITS.IBackgroundCopyJob job;
            _mgr.CreateJob("Quick download", BITS.BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD,
                           out jobGuid, out job);
            try
            {
                job.AddFile(URL, filename);
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                MessageBox.Show(
                    String.Format(Properties.Resources.ErrorBitsException, ex.HResult, ex.Message),
                    Properties.Resources.ErrorTitle
                    );
                job.Cancel();
                return(job);
            }
            catch (System.UnauthorizedAccessException)
            {
                MessageBox.Show(Properties.Resources.ErrorUnauthorizedAccessMessage, Properties.Resources.ErrorUnauthorizedAccessTitle);
                job.Cancel();
                return(job);
            }
            catch (System.ArgumentException ex)
            {
                MessageBox.Show(
                    String.Format(Properties.Resources.ErrorMessage, ex.Message),
                    Properties.Resources.ErrorTitle
                    );
                job.Cancel();
                return(job);
            }

            try
            {
                SetJobProperties(job); // Set job properties as needed
                job.SetNotifyFlags(
                    (UInt32)BitsNotifyFlags.JOB_TRANSFERRED
                    + (UInt32)BitsNotifyFlags.JOB_ERROR);
                job.SetNotifyInterface(this); // Will call JobTransferred, JobError, JobModification based on the notify flags
                job.Resume();
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                MessageBox.Show(
                    String.Format(Properties.Resources.ErrorBitsException, ex.HResult, ex.Message),
                    Properties.Resources.ErrorTitle
                    );
                job.Cancel();
            }
            // Unless there was an error, the job is now running. We can exit and it will continue automatically.
            return(job); // Return the job that was created
        }
Ejemplo n.º 2
0
        private void OnMenuJobCreateNewJob(object sender, RoutedEventArgs e)
        {
            if (_mgr == null)
            {
                return;
            }

            var dlg = new CreateNewJobWindow();

            dlg.Owner = this;
            var result = dlg.ShowDialog();

            if (result.HasValue && result.Value)
            {
                var       jobName = dlg.JobName;
                var       jobType = dlg.JobType;
                BITS.GUID jobId;
                BITS.IBackgroundCopyJob job;
                _mgr.CreateJob(jobName, jobType, out jobId, out job);
                try
                {
                    dlg.SetJobProperties(job);
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    // No need to cancel; the job will show up in the job list and
                    // will be selected. The user should deal with it as they see fit.
                    MessageBox.Show(
                        String.Format(Properties.Resources.ErrorBitsException, ex.HResult, ex.Message),
                        Properties.Resources.ErrorTitle
                        );
                }

                RefreshJobList();

                // Select the newly-created job
                var idx = GetJobIndex(job);
                _uiJobList.SelectedIndex = idx;
            }
        }