protected override void DoWork(DoWorkEventArgs e) { DownloadAllArgs args = e.Argument as DownloadAllArgs; if (args != null) { RestClient client = new RestClient(App.AuthorizationHeader); Action <string> updaterMethod = p => { tbBatchStatus.Text = p; }; int docIndex = 0; int failCount = 0; int okCount = 0; foreach (var attachment in Attachments) { docIndex++; string endpointUrl = Endpoints.Endpoint_RegulatorFacilitySubmittalDocumentQuery.Replace("{CERSUniqueKey}", attachment.CERSUniqueKey); Dispatcher.Invoke(updaterMethod, "Download " + docIndex + " of " + Attachments.Count + " Document(s): " + attachment.CERSUniqueKey + "..."); var result = client.Execute(endpointUrl); if (result.Status == System.Net.HttpStatusCode.OK) { string targetFileName = attachment.CERSID + " - " + attachment.FileName; targetFileName = args.TargetPath + "\\" + targetFileName; try { result.RawData.WriteToFile(targetFileName); Dispatcher.Invoke(updaterMethod, "Document: " + attachment.CERSUniqueKey + " Saved!"); okCount++; } catch (Exception ex) { attachment.Status = "Error: " + ex.Message; Dispatcher.Invoke(updaterMethod, "Document: " + attachment.CERSUniqueKey + " Not Saved! " + ex.Message); failCount++; } } else { attachment.Status = result.Status + " - " + result.StatusDescription; Dispatcher.Invoke(updaterMethod, "Document: " + attachment.CERSUniqueKey + " - " + result.Status + " - " + result.StatusDescription); failCount++; } } Action statusUpdater = () => { tbBatchStatus.Text = okCount + " out of " + Attachments.Count + " downloaded successfully"; }; Dispatcher.Invoke(statusUpdater, null); } }
private void btnDownloadAll_Click(object sender, RoutedEventArgs e) { string targetPath = Utility.SelectFolder(@"C:\temp"); DownloadAllArgs args = new DownloadAllArgs { TargetPath = targetPath }; btnDownloadAll.IsEnabled = false; lvAttachments.IsEnabled = false; Worker.RunWorkerAsync(args); }