void SaveAsText_BackgroundEventHandler(object sender, System.ComponentModel.DoWorkEventArgs e) { try { BackgroundWorker backgroundWorker = sender as BackgroundWorker; List <string> arguments = e.Argument as List <string>; string exportPath = arguments.ElementAt(0); string format = arguments.ElementAt(1); List <string> selectedCountries = arguments.GetRange(2, arguments.Count - 2); int actionCounter = 0; foreach (string countryShortName in selectedCountries) { if (backgroundWorker.CancellationPending) { e.Cancel = true; return; } //user pressed Cancel button backgroundWorker.ReportProgress(Convert.ToInt32((actionCounter++ + 1) / (selectedCountries.Count * 1.0) * 100.0)); bool success = (format == textFormat) ? CountryAdministrator.SaveAsTabDelimitedTextFile(exportPath, countryShortName) : CountryAdministrator.SaveWithLineBreaks(exportPath, countryShortName); if (!success) { e.Result = false; e.Cancel = true; return; } } e.Result = true; } catch (Exception exception) { e.Result = false; e.Cancel = true; UserInfoHandler.ShowException(exception); } }