void worker_DoWork(object sender, DoWorkEventArgs e) { RunWorkArg arg = e.Argument as RunWorkArg; try { int index = 0; foreach (Url item in arg.AllImages) { Thread.Sleep(100); if (this.DownloadWorker.CancellationPending) { arg.Action = Action.Canceled; break; } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(item.Src); request.UserAgent = UserAgent; request.Timeout = Timeout; request.ReadWriteTimeout = Timeout * 6; request.Accept = "*/*"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (Stream responseStream = response.GetResponseStream()) { using (FileStream writer = File.OpenWrite(String.Format("{0}\\{1}", arg.OutputFolder, item.Target))) { responseStream.CopyTo(writer); } } } index++; m_downloadWorker.ReportProgress((int)(((double)index / (double)arg.AllImages.Length) * 100.0)); } if (this.KmlWorker != null) { this.KmlWorker.ReportProgress(100); } } catch (Exception ex) { arg.Action = Action.Error; arg.Error = ex; } e.Result = arg; }
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { RunWorkArg arg = e.Result as RunWorkArg; if (arg.Action == Action.Success) { System.Windows.MessageBox.Show("Tiles are downloaded."); } else if (arg.Action == Action.Canceled) { System.Windows.MessageBox.Show("User Cancel Completed."); } else { if (e.Error != null) { System.Windows.MessageBox.Show(String.Format("Error:{0}.", e.Error.Message)); } else { if (e.Result != null && e.Result is RunWorkArg) { RunWorkArg err = e.Result as RunWorkArg; if (err.Error != null) { System.Windows.MessageBox.Show(String.Format("Error:{0}.", err.Error.Message)); } else { System.Windows.MessageBox.Show("Unknown error occured"); } } else { System.Windows.MessageBox.Show("Unknown error occured"); } } } }