Fix progress bars

This commit is contained in:
pointfeev 2021-08-09 06:41:31 -05:00
parent f2a9c3468f
commit 4df91e0ff8
4 changed files with 15 additions and 27 deletions

View file

@ -6,7 +6,7 @@
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>ini.ico</ApplicationIcon>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<Version>1.0.6.0</Version>
<Version>1.0.6.1</Version>
<PackageIcon>ini.ico</PackageIcon>
<PackageIconUrl />
<Description>Automatically downloads and installs CreamAPI files for programs/games.</Description>

View file

@ -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<ZipArchiveEntry> resources = new List<ZipArchiveEntry>();
@ -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;

View file

@ -140,7 +140,7 @@ namespace CreamInstaller
progress.ProgressChanged += new EventHandler<double>(delegate (object sender, double _progress)
{
label1.Text = $"Updating . . . {(int)_progress}%";
Program.UpdateProgressInstantly(progressBar1, (int)_progress);
progressBar1.Value = (int)_progress;
});
label1.Text = "Updating . . . ";

View file

@ -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);
}
}