/// <summary> /// Creates a <see cref="BackgroundWorker"/> class instance to update the song locations with a user selected path. /// </summary> /// <param name="folderBrowserDialog">An instance to <see cref="Ookii.Dialogs.WinForms.VistaFolderBrowserDialog"/> for the user to select the path.</param> /// <param name="connection">A SQLite connection to use for the procedure.</param> /// <returns>An instance to a <see cref="BackgroundWorker"/> class to handle the operation.</returns> public static BackgroundWorker UpdateSongLocations( VistaFolderBrowserDialog folderBrowserDialog, SQLiteConnection connection) { if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { BackgroundWorker worker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true }; worker.DoWork += (sender, args) => { List <FileEnumeratorFileEntry> files = new List <FileEnumeratorFileEntry>(FileEnumerator .RecurseFiles(folderBrowserDialog.SelectedPath, Constants.Extensions.ToArray()).ToArray()); if (files.Count == 0) { worker.ReportProgress(100); return; } int fileCount = files.Count; long songCount = GetScalar <long>(Song.GenerateCountSentence(false), connection); int progress = 0; int previousProgress = -1; List <Song> songs = new List <Song>(); using (SQLiteCommand command = new SQLiteCommand(Song.GenerateSelectSentence(false), connection)) { using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var percentage = progress * 50 / (int)songCount; if (percentage > previousProgress) { worker.ReportProgress(percentage); previousProgress = percentage; } songs.Add(Song.FromSqLiteDataReader(reader)); progress++; if (worker.CancellationPending) { return; } } } } progress = 0; foreach (var file in files) { try { var fileInfo = new FileInfo(file.FileNameWithPath); var song = songs.FirstOrDefault(f => f.FileSize == fileInfo.Length && f.FileNameNoPath == fileInfo.Name); if (song == null) { song = songs.FirstOrDefault(f => f.Filename == fileInfo.FullName); if (song != null) { song.FileSize = (int)fileInfo.Length; } else { song = songs.FirstOrDefault(f => f.FileNameNoPath == fileInfo.Name); if (song != null) { song.FileSize = (int)fileInfo.Length; } } } var percentage = 50 + progress * 50 / fileCount; if (percentage > previousProgress) { worker.ReportProgress(percentage); previousProgress = percentage; } if (song == null) { continue; } song.Filename = file.FileNameWithPath; ExecuteSql(song.GenerateInsertUpdateSqlSentence(false), connection); progress++; if (worker.CancellationPending) { return; } } catch (Exception ex) { // log the exception.. ExceptionLogger.LogError(ex); } } worker.ReportProgress(100); }; return(worker); } return(null); }