private static void CopyTemplate(string sourceFileName, string destFileName, Func <bool> shouldCancel, Action <int> delay, Action <BackgroundCopyJobState, byte> report, Action <BackgroundCopyFileCollection> add) { var type = (Uri.TryCreate(destFileName, UriKind.Absolute, out var uri) && !uri.IsFile) ? BackgroundCopyJobType.Upload : BackgroundCopyJobType.Download; using (var job = Jobs.Add("Temp" + Guid.NewGuid().ToString(), "", type)) { job.DisableNotifications = true; add(job.Files); BackgroundCopyJobState state = BackgroundCopyJobState.Connecting; job.Resume(); do { switch (state = job.State) { case BackgroundCopyJobState.Queued: case BackgroundCopyJobState.Connecting: case BackgroundCopyJobState.Transferring: case BackgroundCopyJobState.Suspended: ReportProgress(); break; case BackgroundCopyJobState.Error: case BackgroundCopyJobState.TransientError: throw job.LastError; case BackgroundCopyJobState.Transferred: ReportProgress(); job.Complete(); return; case BackgroundCopyJobState.Acknowledged: case BackgroundCopyJobState.Cancelled: return; default: throw new InvalidOperationException("Unknown job state"); } if (shouldCancel()) { job.Cancel(); break; } delay(1000); void ReportProgress() { report?.Invoke(state, job.Progress.PercentComplete); } } while (state != BackgroundCopyJobState.Transferred && state != BackgroundCopyJobState.Error && state != BackgroundCopyJobState.TransientError); } }
private void CheckNotifier() { if (m_notifier == null) { this.NotifyFlags = 0; m_notifier = new Notifier(this); } BackgroundCopyJobState st = this.State; if (st != BackgroundCopyJobState.Acknowledged && st != BackgroundCopyJobState.Cancelled) { m_ijob.SetNotifyInterface(m_notifier); } }
private void HandleCOMException(COMException cex) { BackgroundCopyJobState state = BackgroundCopyJob.GetState(m_ijob); if (state == BackgroundCopyJobState.Error || state == BackgroundCopyJobState.TransientError) { Interop.IBackgroundCopyError pErr; m_ijob.GetError(out pErr); throw new BackgroundCopyException(pErr); } else { throw new BackgroundCopyException(cex); } }
public static TransferStatus ToTranferStatus(this BackgroundCopyJobState state) => (TransferStatus)state;