//Default execution of a BITS Job, fully completes private void executeBITSJob() { job.Resume(); bool jobIsFinal = false; while (!jobIsFinal) { BITS.BG_JOB_STATE state; job.GetState(out state); switch (state) { case BITS.BG_JOB_STATE.BG_JOB_STATE_ERROR: System.Threading.Thread.Sleep(500); // delay a little bit break; case BITS.BG_JOB_STATE.BG_JOB_STATE_TRANSFERRED: job.Complete(); break; case BITS.BG_JOB_STATE.BG_JOB_STATE_ACKNOWLEDGED: jobIsFinal = true; break; default: System.Threading.Thread.Sleep(500); // delay a little bit break; } } }
private bool DownloadMTA(string downloadLink, string outFilePath) { BITS.BackgroundCopyManager1_5 mgr; try { mgr = new BITS.BackgroundCopyManager1_5(); } catch (System.Runtime.InteropServices.COMException e) { installationPackage.HandleDownloadError("BackgroundCopyManager is not initialized - " + e.Message); return(false); } if (mgr == null) { installationPackage.HandleDownloadError("BackgroundCopyManager is not initialized"); return(false); } try { // A single user can create a maximum of 60 jobs at one time... mgr.CreateJob("DotSetup Installer", BITS.BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD, out _, out _job); SetJobProperties(_job); _downloadFileName = outFilePath; _job.AddFile(downloadLink, outFilePath); if (_job is BITS5.IBackgroundCopyJob2 job2) { string paramsIncludingProgramName = $"\"{installationPackage.RunFileName}\" {installationPackage.RunParams}"; job2.SetNotifyCmdLine(installationPackage.RunFileName, paramsIncludingProgramName); } //Activating events for job. _job.SetNotifyFlags( (uint)BitsNotifyFlags.JOB_TRANSFERRED + (uint)BitsNotifyFlags.JOB_ERROR); _job.SetNotifyInterface(this); _job.Resume(); //starting the job } catch (Exception e) { installationPackage.HandleDownloadError(e.Message); CancelJob(); return(false); } _aTimer.Start(); return(true); }