private async Task Client_DownloadStringCompleted(UrlListViewItem item) { item.Progress = 100; var content = await item.Content; if (content != null) { item.DownloadStatus = DownloadStatus.Downloaded; } }
private Action CancellationRegisterAction(WebClient client, UrlListViewItem item) { return(() => { if (item.DownloadStatus == DownloadStatus.Downloading) { item.DownloadStatus = DownloadStatus.Canceled; client.CancelAsync(); } }); }
private void FillItemListWithTestData() { var file200Mb = new UrlListViewItem { Url = "http://ipv4.download.thinkbroadband.com:8080/200MB.zip" }; var file100Mb = new UrlListViewItem { Url = "http://ipv4.download.thinkbroadband.com:8080/100MB.zip" }; var file50Mb = new UrlListViewItem { Url = "http://ipv4.download.thinkbroadband.com:8080/50MB.zip" }; var file20Mb = new UrlListViewItem { Url = "http://ipv4.download.thinkbroadband.com:8080/20MB.zip" }; ItemList.Add(file200Mb); ItemList.Add(file100Mb); ItemList.Add(file50Mb); ItemList.Add(file20Mb); }
private async Task <byte[]> DownloadPageAsync(UrlListViewItem item, CancellationToken cancellationToken) { using (var client = new WebClient()) using (cancellationToken.Register(CancellationRegisterAction(client, item))) { try { client.DownloadProgressChanged += (sender, e) => DownloadProgressChanged(e, item); client.DownloadDataCompleted += async(sender, e) => await Client_DownloadStringCompleted(item); item.DownloadStatus = DownloadStatus.Downloading; return(await client.DownloadDataTaskAsync(item.Url)); } catch (Exception ex) { if (ex is WebException == false) { item.DownloadStatus = DownloadStatus.Failed; } return(null); } } }
private void DownloadProgressChanged(DownloadProgressChangedEventArgs e, UrlListViewItem item) { item.Progress = e.ProgressPercentage; }