From 565d2123b9059cebfdde5e1d54d17e2a30d2cb3c Mon Sep 17 00:00:00 2001 From: ZestyTS Date: Tue, 25 Jul 2017 01:15:39 -0400 Subject: [PATCH] Added a Progress Bar and a Background Worker --- RetroManager/CompressRom.Designer.cs | 11 +++++ RetroManager/CompressRom.cs | 65 +++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/RetroManager/CompressRom.Designer.cs b/RetroManager/CompressRom.Designer.cs index 913127c..8a6af31 100644 --- a/RetroManager/CompressRom.Designer.cs +++ b/RetroManager/CompressRom.Designer.cs @@ -57,6 +57,7 @@ private void InitializeComponent() this.ttef = new System.Windows.Forms.ToolTip(this.components); this.tte = new System.Windows.Forms.ToolTip(this.components); this.lbEmulator = new System.Windows.Forms.Label(); + this.pb = new System.Windows.Forms.ProgressBar(); this.gbEmulator.SuspendLayout(); this.SuspendLayout(); // @@ -322,11 +323,20 @@ private void InitializeComponent() this.lbEmulator.TabIndex = 14; this.lbEmulator.Text = "Emulators"; // + // pb + // + this.pb.Location = new System.Drawing.Point(133, 273); + this.pb.Name = "pb"; + this.pb.Size = new System.Drawing.Size(203, 46); + this.pb.TabIndex = 14; + this.pb.Visible = false; + // // CompressRom // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(354, 335); + this.Controls.Add(this.pb); this.Controls.Add(this.gbEmulator); this.Controls.Add(this.btnStart); this.Controls.Add(this.cbExtract); @@ -373,5 +383,6 @@ private void InitializeComponent() private System.Windows.Forms.ToolTip ttef; private System.Windows.Forms.ToolTip tte; private System.Windows.Forms.Label lbEmulator; + private System.Windows.Forms.ProgressBar pb; } } \ No newline at end of file diff --git a/RetroManager/CompressRom.cs b/RetroManager/CompressRom.cs index e16ec8c..5ee8df9 100644 --- a/RetroManager/CompressRom.cs +++ b/RetroManager/CompressRom.cs @@ -1,7 +1,9 @@ using System; using System.Collections; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; +using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; @@ -10,12 +12,19 @@ namespace RetroManager { public partial class CompressRom : Form { + private readonly BackgroundWorker _bw = new BackgroundWorker(); public CompressRom() { InitializeComponent(); + + _bw.WorkerReportsProgress = true; + _bw.WorkerSupportsCancellation = true; + _bw.DoWork += BtnStart_Work; + _bw.ProgressChanged += ProgressUpdate; + _bw.RunWorkerCompleted += WorkerCompleted; } - private void BtnStart_Click(object sender, EventArgs e) + private void BtnStart_Work(object sender, EventArgs e) { const string sixfour = @"C:\Program Files\7-Zip\7z.exe"; const string threetwo = @"C:\Program Files (x86)\7-Zip\7z.exe"; @@ -85,19 +94,55 @@ private void BtnStart_Click(object sender, EventArgs e) { emulators.Add(c.Text.Replace(" ", string.Empty).ToLower()); } - - foreach (string emu in emulators) - foreach (var ext in extensions[emu]) - { - var roms = Directory.GetFiles(reader, "*." + ext, SearchOption.AllDirectories); - foreach (var rom in roms) + using (var gr = pb.CreateGraphics()) + { + foreach (string emu in emulators) + foreach (var ext in extensions[emu]) { - p.Arguments = "a -tzip -mx9 -mm=Deflate64 " + $@"""{Path.ChangeExtension(rom, ".zip")}""" + " " + $@"""{rom}"""; - var x = Process.Start(p); - x.WaitForExit(); + var roms = Directory.GetFiles(reader, "*." + ext, SearchOption.AllDirectories); + var i = 0; + foreach (var rom in roms) + { + p.Arguments = "a -tzip -mx9 -mm=Deflate64 " + $@"""{Path.ChangeExtension(rom, ".zip")}""" + + " " + $@"""{rom}"""; + var x = Process.Start(p); + x.WaitForExit(); + var percentage = (i + 1) * 100 / roms.Length; + i++; + _bw.ReportProgress(percentage); + } } } + } + + private void BtnStart_Click(object sender, EventArgs e) + { + if (_bw.IsBusy) + return; + btnStart.Enabled = false; + pb.Visible = true; + _bw.RunWorkerAsync(); + } + + private void ProgressUpdate(object sender, ProgressChangedEventArgs e) + { + pb.Value = e.ProgressPercentage; + } + + private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + { + if (e.Error != null) + { + MessageBox.Show(@"Something went wrong\n" + @"Error: " + e.Error.Message + @": " + e.Error.InnerException); + } + else + { + MessageBox.Show(@"Zipping Complete!"); + } + pb.Value = 0; + pb.Visible = false; + btnStart.Enabled = true; } private void BtnBrowse_Click(object sender, EventArgs e)