From 4df91e0ff88cfc518e3494d3948049fd8c6f1f39 Mon Sep 17 00:00:00 2001 From: pointfeev Date: Mon, 9 Aug 2021 06:41:31 -0500 Subject: [PATCH] Fix progress bars --- CreamInstaller/CreamInstaller.csproj | 2 +- CreamInstaller/InstallForm.cs | 22 +++++++++++++--------- CreamInstaller/MainForm.cs | 2 +- CreamInstaller/Program.cs | 16 ---------------- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/CreamInstaller/CreamInstaller.csproj b/CreamInstaller/CreamInstaller.csproj index e25c4dc..4b10c40 100644 --- a/CreamInstaller/CreamInstaller.csproj +++ b/CreamInstaller/CreamInstaller.csproj @@ -6,7 +6,7 @@ true ini.ico true - 1.0.6.0 + 1.0.6.1 ini.ico Automatically downloads and installs CreamAPI files for programs/games. diff --git a/CreamInstaller/InstallForm.cs b/CreamInstaller/InstallForm.cs index f4e37f2..81df096 100644 --- a/CreamInstaller/InstallForm.cs +++ b/CreamInstaller/InstallForm.cs @@ -23,9 +23,13 @@ namespace CreamInstaller logTextBox.BackColor = LogColor.Background; } + private int OperationsCount; + private int CompleteOperationsCount; public void UpdateProgress(int progress) { - Program.UpdateProgressInstantly(userProgressBar, progress); + int value = (int)((float)((float)CompleteOperationsCount / (float)OperationsCount) * 100) + (progress / OperationsCount); + if (value < userProgressBar.Value) { return; } + userProgressBar.Value = value; } public void UpdateUser(string text, Color color, bool log = true) @@ -41,7 +45,7 @@ namespace CreamInstaller logTextBox.AppendText(userInfoLabel.Text, color); } } - + private async Task OperateFor(ProgramSelection selection) { UpdateProgress(0); @@ -70,9 +74,6 @@ namespace CreamInstaller Program.OutputTask = Program.MegaApiClient.DownloadFileAsync(selection.DownloadNode, Program.OutputFile, progress, Program.CancellationTokenSource.Token); await Program.OutputTask; UpdateUser($"Downloaded file: {Program.OutputFile}", LogColor.Resource); - UpdateProgress(100); - - UpdateProgress(0); UpdateUser("Searching for CreamAPI files in downloaded archive . . . ", LogColor.Operation); string resourcePath = null; List resources = new List(); @@ -102,14 +103,10 @@ namespace CreamInstaller { throw new Exception($"Unable to find CreamAPI files in downloaded archive: {Program.OutputFile}"); } - UpdateProgress(100); - if (!Program.IsProgramRunningDialog(this, selection)) { throw new OperationCanceledException(); } - - UpdateProgress(0); UpdateUser("Installing CreamAPI files for " + selection.ProgramName + " . . . ", LogColor.Operation); int currentFileCount = 0; foreach (string directory in selection.SteamApiDllDirectories) @@ -174,6 +171,9 @@ namespace CreamInstaller private async Task Operate() { + OperationsCount = Program.ProgramSelections.FindAll(selection => selection.Enabled).Count; + CompleteOperationsCount = 0; + foreach (ProgramSelection selection in Program.ProgramSelections.ToList()) { if (Program.Canceled) @@ -201,6 +201,8 @@ namespace CreamInstaller { UpdateUser($"Operation failed for {selection.ProgramName}: " + exception.Message, LogColor.Error); } + + ++CompleteOperationsCount; } Program.Cleanup(logout: false); @@ -228,6 +230,7 @@ namespace CreamInstaller retryButton.Enabled = false; cancelButton.Enabled = true; reselectButton.Enabled = false; + userProgressBar.Value = userProgressBar.Minimum; try { await Operate(); @@ -238,6 +241,7 @@ namespace CreamInstaller UpdateUser("CreamAPI download and/or installation failed: " + exception.Message, LogColor.Error); retryButton.Enabled = true; } + userProgressBar.Value = userProgressBar.Maximum; acceptButton.Enabled = true; cancelButton.Enabled = false; reselectButton.Enabled = true; diff --git a/CreamInstaller/MainForm.cs b/CreamInstaller/MainForm.cs index b2b6590..359d254 100644 --- a/CreamInstaller/MainForm.cs +++ b/CreamInstaller/MainForm.cs @@ -140,7 +140,7 @@ namespace CreamInstaller progress.ProgressChanged += new EventHandler(delegate (object sender, double _progress) { label1.Text = $"Updating . . . {(int)_progress}%"; - Program.UpdateProgressInstantly(progressBar1, (int)_progress); + progressBar1.Value = (int)_progress; }); label1.Text = "Updating . . . "; diff --git a/CreamInstaller/Program.cs b/CreamInstaller/Program.cs index 819c501..d6f5571 100644 --- a/CreamInstaller/Program.cs +++ b/CreamInstaller/Program.cs @@ -83,32 +83,21 @@ namespace CreamInstaller public static Task OutputTask; public static string OutputFile; - public static void UpdateProgressInstantly(ProgressBar progressBar, int progress) - { - progressBar.Maximum++; - progressBar.Value = progress + 1; - progressBar.Value = progress; - progressBar.Maximum--; - } - public static void Cleanup(bool cancel = true, bool logout = true) { Canceled = cancel; if (OutputArchive != null || CancellationTokenSource != null || OutputTask != null || OutputFile != null) { - InstallForm?.UpdateProgress(0); InstallForm?.UpdateUser("Cleaning up . . . ", LogColor.Cleanup); } if (OutputArchive != null) { OutputArchive.Dispose(); OutputArchive = null; - InstallForm?.UpdateProgress(25); } if (CancellationTokenSource != null) { CancellationTokenSource.Cancel(); - InstallForm?.UpdateProgress(40); } if (OutputTask != null) { @@ -119,13 +108,11 @@ namespace CreamInstaller catch (AggregateException) { } OutputTask.Dispose(); OutputTask = null; - InstallForm?.UpdateProgress(50); } if (CancellationTokenSource != null) { CancellationTokenSource.Dispose(); CancellationTokenSource = null; - InstallForm?.UpdateProgress(75); } if (OutputFile != null && File.Exists(OutputFile)) { @@ -139,13 +126,10 @@ namespace CreamInstaller } OutputFile = null; } - InstallForm?.UpdateProgress(100); if (logout && MegaApiClient != null && MegaApiClient.IsLoggedIn) { - InstallForm?.UpdateProgress(0); InstallForm?.UpdateUser("Logging out of MEGA . . . ", LogColor.Cleanup); MegaApiClient.Logout(); - InstallForm?.UpdateProgress(100); } }