private void GenerateBarCodes(object sender, RoutedEventArgs rea) { var worker = new BackgroundWorker(); worker.DoWork += (send, ev) => { try { var saveFileDialog = new VistaFolderBrowserDialog(); if (saveFileDialog.ShowDialog() == true) { BarcodeGenerator.CreateDmCodes(saveFileDialog.SelectedPath, MasterDataSet, this); } } catch (WriterException e) { Console.WriteLine($@"Could not generate Barcode, WriterException :: {e.Message}"); } catch (IOException e) { Console.WriteLine($@"Could not generate Barcode, IOException :: {e.Message}"); } }; worker.RunWorkerAsync(); worker.Dispose(); }