Skip to content

Commit

Permalink
Implemented the ussage of background worker so that the application w…
Browse files Browse the repository at this point in the history
…ont freeze while extracting
  • Loading branch information
Luciano Godoy committed Sep 10, 2018
1 parent 8455254 commit 00718f3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
24 changes: 22 additions & 2 deletions Osu-Mp3-Extractor/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ public partial class MainForm : Form
public MainForm()
{
InitializeComponent();

backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
backgroundWorker1.WorkerReportsProgress = true;
}

//Startup//
private void MainForm_Load(object sender, EventArgs e)
{
Expand Down Expand Up @@ -80,7 +86,21 @@ private void extractButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exctract all of the songs within the selection?" + Environment.NewLine + Environment.NewLine + "This cannont be undone", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
ExtractSongs();
//show the progress bar
progressBar1.Visible = true;
progressBar1.Maximum = songsext.SongsList.Count();

//hides the buttons
addallButton.Enabled = false;
clearButton.Enabled = false;
addButton.Enabled = false;
folderButton.Enabled = false;
searchTextBox.Enabled = false;
songsListBox.Enabled = false;
extractqueueListBox.Enabled = false;

//Extracts songs on background
backgroundWorker1.RunWorkerAsync();
}
}
private void clearButton_Click(object sender, EventArgs e)
Expand Down
35 changes: 24 additions & 11 deletions Osu-Mp3-Extractor/MainFormUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.RegularExpressions;
using System.Drawing;
using System.IO;
using System.ComponentModel;

namespace Osu_Mp3_Extractor
{
Expand All @@ -29,6 +30,7 @@ public partial class MainForm
private List<Song> SongsFiltered;
private List<Song> SongsExtract;
private GetSongs songsext;
private BackgroundWorker backgroundWorker1;

//Methods//
private void FillSongsFilteredList(string searchString)
Expand Down Expand Up @@ -77,12 +79,8 @@ private void SetFolder()
else
this.Close();
}
private void ExtractSongs()
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//show the progress bar
progressBar1.Visible = true;
progressBar1.Maximum = songsext.SongsList.Count();

//Create Cover Object
TagLib.Picture pic = new TagLib.Picture();
pic.Type = TagLib.PictureType.FrontCover;
Expand All @@ -104,7 +102,7 @@ private void ExtractSongs()
//Applying the modifications
file.Tag.Title = songn.Title; //title
file.Tag.AlbumArtists = songn.Artist.Split(new char[] { ';' }); //Artist
file.Tag.Album = "osu!";
file.Tag.Album = "osu!"; //Album

//Applying the cover
if (songn.ImagePath != "" && System.IO.File.Exists(songn.ImagePath))
Expand All @@ -113,7 +111,7 @@ private void ExtractSongs()
}
else
{
if(System.IO.File.Exists(imgPath))
if (System.IO.File.Exists(imgPath))
{
pic.Data = TagLib.ByteVector.FromPath(imgPath);
}
Expand All @@ -123,17 +121,24 @@ private void ExtractSongs()
pic.Data = TagLib.ByteVector.FromPath(imgPath);
}
}

file.Tag.Pictures = new TagLib.IPicture[] { pic };

//Save the mp3
file.Save();

//increment progressbar
progressBar1.PerformStep();
//Update Prograss Bar
backgroundWorker1.ReportProgress(songn.Code);
}
}

}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
PrintSongDetails(e.ProgressPercentage);
progressBar1.PerformStep();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
//Finishes the process
progressBar1.Visible = false;
System.IO.File.Delete(txtPath);
using (StreamWriter sw = System.IO.File.CreateText(txtPath))
Expand All @@ -142,6 +147,14 @@ private void ExtractSongs()
sw.WriteLine("SongsPath=" + songsPath);
sw.WriteLine("Extracts=" + extractions);
}

addallButton.Enabled = true;
clearButton.Enabled = true;
addButton.Enabled = true;
folderButton.Enabled = true;
searchTextBox.Enabled = true;
songsListBox.Enabled = true;
extractqueueListBox.Enabled = true;
}
private void Check()
{
Expand Down
Binary file modified Release.rar
Binary file not shown.

0 comments on commit 00718f3

Please sign in to comment.