public Task<byte[]> Download(string uri, IDownloadNotifier notifier, CancellationToken cancellationToken) { //var req = (HttpWebRequest)WebRequest.Create(uri); //var res = (HttpWebResponse)req.GetResponse(); //return StreamUtil.ReadFully(res.GetResponseStream()); var tcs = new TaskCompletionSource<byte[]>(); var wc = new WebClient(); if (notifier != null) { notifier.ReportStart(); wc.DownloadProgressChanged += (s, ea) => { notifier.ReportProgress(ea.BytesReceived, ea.TotalBytesToReceive); if (cancellationToken.IsCancellationRequested) wc.CancelAsync(); }; } wc.DownloadDataCompleted += (s, ea) => { var d = uri; if (notifier != null) notifier.ReportFinish(); if (ea.Error != null) tcs.SetException(ea.Error); else if (ea.Cancelled) tcs.SetCanceled(); else tcs.SetResult(ea.Result); }; wc.DownloadDataAsync(new Uri(uri)); return tcs.Task; }
public Task<byte[]> Download(string uri, IDownloadNotifier notifier, CancellationToken cancellationToken) { var tcs = new TaskCompletionSource<byte[]>(); tcs.SetResult(_returnBytes); return tcs.Task; }