private string DownloadQueryResults(string sourceUrl) { if (!string.IsNullOrEmpty(sourceUrl)) { string tempZipDir = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName())); string tempZipFile = tempZipDir + ".zip"; WebClient webClient = null; bool canceled = false; try { webClient = new WebClient(); bool downloadCompleted = false; webClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e) { string msg; if (IsCancelRequested()) { OnCancelRequested(); if (!canceled) { webClient.CancelAsync(); } canceled = true; return; } if (e.BytesReceived < 1000 * 1024) { msg = string.Format("Retrieving images ({0:0.00}KB)", ((float)e.BytesReceived) / 1024); } else { msg = string.Format("Retrieving images ({0:0.00}MB)", ((float)e.BytesReceived) / 1000 / 1024); } OnProgressUpdated(msg); //OnResultUpdated(Result); }; webClient.DownloadFileCompleted += delegate(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { downloadCompleted = true; }; webClient.DownloadFileAsync(new Uri(sourceUrl), tempZipFile); //webClient.DownloadFile(sourceUrl, tempZipFile); while (!downloadCompleted) { System.Threading.Thread.Sleep(500); } if (IsCancelRequested()) { OnCancelRequested(); canceled = true; } if (!canceled) { OnProgressUpdated("Processing received images"); //Result.ProgressMessage = "Processing received images"; //OnResultUpdated(Result); try { ZipUtil.UnZipFiles(tempZipFile, tempZipDir, "", false, true); File.Delete(tempZipFile); } catch (Exception ex) { Platform.Log(LogLevel.Error, "Error processing received images", ex); OnError("Error processing received images"); return(null); } return(tempZipDir); } } finally { if (webClient != null) { webClient.Dispose(); } } } return(null); }
private string DownloadQueryResults(string sourceUrl, IBackgroundTaskContext context) { if (!string.IsNullOrEmpty(sourceUrl)) { string tempZipDir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetRandomFileName())); string tempZipFile = tempZipDir + ".zip"; WebClient webClient = null; bool canceled = false; try { webClient = new WebClient(); bool downloadCompleted = false; webClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e) { if (context != null) { string progressMsg; if (e.BytesReceived < 1000 * 1024) { progressMsg = string.Format("Retrieving images ({0:0.00}KB)", ((float)e.BytesReceived) / 1024); } else { progressMsg = string.Format("Retrieving images ({0:0.00}MB)", ((float)e.BytesReceived) / 1000 / 1024); } BackgroundTaskProgress progress = new BackgroundTaskProgress(1, 3, progressMsg); context.ReportProgress(progress); if (context.CancelRequested) { if (!canceled) { webClient.CancelAsync(); } context.Cancel(); canceled = true; } } }; webClient.DownloadFileCompleted += delegate(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { downloadCompleted = true; }; webClient.DownloadFileAsync(new Uri(sourceUrl), tempZipFile); //webClient.DownloadFile(sourceUrl, tempZipFile); while (!downloadCompleted) { System.Threading.Thread.Sleep(500); } if (!canceled) { if (context != null) { BackgroundTaskProgress progress = new BackgroundTaskProgress(1, 3, "Processing received images"); context.ReportProgress(progress); if (context.CancelRequested) { context.Cancel(); canceled = true; } } ZipUtil.UnZipFiles(tempZipFile, tempZipDir, "", false, true); try { File.Delete(tempZipFile); } catch (Exception) { } return(tempZipDir); } } finally { if (webClient != null) { webClient.Dispose(); } } } return(null); }