public async void Start() { try { var downloads = await BackgroundDownloader .GetCurrentDownloadsAsync() .AsTask(); foreach (var dl in downloads) { dl.Resume(); } var uploads = await BackgroundUploader .GetCurrentUploadsAsync() .AsTask(); foreach (var ul in uploads) { ul.StartAsync(); } } catch (Exception ex) { Log.Write(ex); } }
// 加载全部上传任务 private async Task LoadUploadAsync() { IReadOnlyList <UploadOperation> uploads = null; try { // 获取所有后台上传任务 uploads = await BackgroundUploader.GetCurrentUploadsAsync(); } catch (Exception ex) { WriteLine(ex.ToString()); return; } if (uploads.Count > 0) { List <Task> tasks = new List <Task>(); foreach (UploadOperation upload in uploads) { // 监视指定的后台上传任务 tasks.Add(HandleUploadAsync(upload, false)); } await Task.WhenAll(tasks); } }
public override IObservable <HttpTransfer> WhenUpdated() => Observable.Create <HttpTransfer>(async(ob, ct) => { var downloads = await BackgroundDownloader .GetCurrentDownloadsAsync() .AsTask(); foreach (var download in downloads) { download .AttachAsync() .AsTask( ct, new Progress <DownloadOperation>(_ => ob.OnNext(download.FromNative()) ) ); } var uploads = await BackgroundUploader .GetCurrentUploadsAsync() .AsTask(); foreach (var upload in uploads) { upload .AttachAsync() .AsTask( ct, new Progress <UploadOperation>(_ => ob.OnNext(upload.FromNative()) ) ); } });
public HttpTransferTasks() { BackgroundUploader .GetCurrentUploadsAsync() .AsTask() .ContinueWith(result => { foreach (var task in result.Result) { var config = new TaskConfiguration(task.RequestedUri.ToString(), task.SourceFile.Path) { HttpMethod = task.Method, UseMeteredConnection = task.CostPolicy != BackgroundTransferCostPolicy.UnrestrictedOnly }; this.Add(new UploadHttpTask(config, task, true)); } }); BackgroundDownloader .GetCurrentDownloadsAsync() .AsTask() .ContinueWith(result => { foreach (var task in result.Result) { var config = new TaskConfiguration(task.RequestedUri.ToString()) { HttpMethod = task.Method, UseMeteredConnection = task.CostPolicy != BackgroundTransferCostPolicy.UnrestrictedOnly }; this.Add(new DownloadHttpTask(config, task, true)); } }); }
public async Task CancelAll() { var uploads = await BackgroundUploader.GetCurrentUploadsAsync(); foreach (var upload in uploads) { upload.AttachAsync().Cancel(); } }
protected override async Task <IEnumerable <HttpTransfer> > GetUploads(QueryFilter filter) { var items = await BackgroundUploader .GetCurrentUploadsAsync() .AsTask(); return(items .Select(x => x.FromNative()) .ToList()); }
protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e) { base.OnNavigatingFrom(e); var currentUploads = await BackgroundUploader.GetCurrentUploadsAsync(); if (currentUploads.Count > 0) { _cts.Cancel(); _cts.Dispose(); } }
// Enumerate the uploads that were going on in the background while the app was closed. private async Task DiscoverActiveUploadsAsync() { IReadOnlyList <UploadOperation> uploads = null; try { uploads = await BackgroundUploader.GetCurrentUploadsAsync(); } catch (Exception ex) { if (!IsExceptionHandled("Discovery error", ex)) { throw; } return; } Log("Loading background uploads: " + uploads.Count); if (uploads.Count > 0) { List <Task> tasks = new List <Task>(); foreach (UploadOperation upload in uploads) { Log(String.Format(CultureInfo.CurrentCulture, "Discovered background upload: {0}, Status: {1}", upload.Guid, upload.Progress.Status)); // Attach progress and completion handlers. tasks.Add(HandleUploadAsync(upload, false)); } // Don't await HandleUploadAsync() in the foreach loop since we would attach to the second // upload only when the first one completed; attach to the third upload when the second one // completes etc. We want to attach to all uploads immediately. // If there are actions that need to be taken once uploads complete, await tasks here, outside // the loop. await Task.WhenAll(tasks); } }
// Enumerate the uploads that were going on in the background while the app was closed. private async Task DiscoverActiveUploadsAsync() { IReadOnlyList <UploadOperation> uploads = null; try { uploads = await BackgroundUploader.GetCurrentUploadsAsync(); } catch (Exception ex) { if (!IsExceptionHandled("Discovery error", ex)) { var fileUploadResponse = new FileUploadResponse(ex.ToString(), -1, string.Empty, null); FileUploadError(this, fileUploadResponse); } return; } if (uploads.Count > 0) { List <Task> tasks = new List <Task>(); foreach (UploadOperation upload in uploads) { // Attach progress and completion handlers. tasks.Add(HandleUploadAsync(upload, false)); } // Don't await HandleUploadAsync() in the foreach loop since we would attach to the second // upload only when the first one completed; attach to the third upload when the second one // completes etc. We want to attach to all uploads immediately. // If there are actions that need to be taken once uploads complete, await tasks here, outside // the loop. await Task.WhenAll(tasks); } }