Skip to content

Commit

Permalink
Provide upgrade progress indication.
Browse files Browse the repository at this point in the history
Resolve UI freeze.
#1837
  • Loading branch information
phw198 committed Apr 18, 2024
1 parent a44b55a commit 6eced8f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 16 deletions.
30 changes: 21 additions & 9 deletions src/OutlookGoogleCalendarSync/Forms/UpdateInfo.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 94 additions & 1 deletion src/OutlookGoogleCalendarSync/Forms/UpdateInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using log4net;

Expand All @@ -8,6 +9,7 @@ public partial class UpdateInfo : Form {

private String version = "";
private String anchorRequested = "";
private DialogResult optionChosen = DialogResult.None;
private String htmlHead = @"
<html>
<head>
Expand Down Expand Up @@ -36,7 +38,7 @@ public UpdateInfo(String releaseVersion, String releaseType, String html, out Di
String githubReleaseNotes = Program.OgcsWebsite + "/release-notes";
anchorRequested = "v" + releaseVersion.Replace(".", "") + "---" + releaseType;
log.Debug("Browser anchor: " + anchorRequested);
llViewOnGithub.Tag = githubReleaseNotes +"#"+ anchorRequested;
llViewOnGithub.Tag = githubReleaseNotes + "#" + anchorRequested;
llViewOnGithub.Visible = true;

} else {
Expand All @@ -53,6 +55,8 @@ public UpdateInfo(String releaseVersion, String releaseType, String html, out Di
OGCSexception.Analyse(ex);
dr = OgcsMessageBox.Show("A new " + (releaseType == "alpha" ? "alpha " : "") + "release of OGCS is available.\nWould you like to upgrade to v" +
releaseVersion + " now?", "OGCS Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
} finally {
optionChosen = dr;
}
}

Expand Down Expand Up @@ -89,5 +93,94 @@ private void btSkipVersion_Click(object sender, EventArgs e) {
private void llViewOnGithub_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
Helper.OpenBrowser(llViewOnGithub.Tag.ToString());
}

#region Upgrading
public void PrepareForUpgrade() {
btUpgrade.Text = "Upgrading";
btSkipVersion.Visible = false;
btLater.Visible = false;

Point frmLocation = this.DesktopLocation;
this.Visible = true;
this.DesktopLocation = frmLocation;
Application.DoEvents();

//Copied from InitializeComponent()
//Recreating webBrowser
this.webBrowser = new WebBrowser();
this.wbPanel.Controls.Add(this.webBrowser);
this.webBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser.Location = new System.Drawing.Point(0, 0);
this.webBrowser.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser.Name = "webBrowser";
this.webBrowser.ScriptErrorsSuppressed = true;
this.webBrowser.Size = new System.Drawing.Size(465, 166);
this.webBrowser.TabIndex = 0;
this.webBrowser.WebBrowserShortcutsEnabled = false;
this.webBrowser.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser_Navigating);
this.Controls.Add(wbPanel);

this.webBrowser.Navigate("about:blank");
webBrowser.Document.Write(cachedWebPage);
this.webBrowser.Refresh(WebBrowserRefreshOption.Completely);
while (webBrowser.ReadyState != WebBrowserReadyState.Complete) {
System.Threading.Thread.Sleep(250);
Application.DoEvents();
}
Application.DoEvents();
}

private int previousProgress = 0;

public void ShowUpgradeProgress(int i) {
log.Debug($"Update progress: {i}%");

Rectangle rect = new Rectangle(0, 0, 0, 0);
for (int j = previousProgress; j <= (btUpgrade.Width * i / 100); j++) {
Bitmap bmp = new Bitmap(btUpgrade.Width, btUpgrade.Height);
Graphics g = Graphics.FromImage(bmp);
rect = new Rectangle(0, 0, Math.Max(5, j), bmp.Height);
using (var b1 = new System.Drawing.SolidBrush(Color.LimeGreen))
g.FillRectangle(b1, rect);
btUpgrade.BackgroundImage = bmp;
Application.DoEvents();
System.Threading.Thread.Sleep(50);
}
previousProgress = rect.Width;
}

public void UpgradeCompleted() {
if (this.Visible) btUpgrade.Text = "Restart";
else
OgcsMessageBox.Show("The application has been updated and will now restart.",
"OGCS successfully updated!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public Boolean AwaitingRestart {
get { return btUpgrade.Text == "Restart" && this.Visible; }
}

private String cachedWebPage;

private void btUpgrade_Click(object sender, EventArgs e) {
if (this.btUpgrade.Text == "Upgrading") return;

if (this.AwaitingRestart && !this.IsDisposed) {
this.btUpgrade.Text = "Restarting";
this.Dispose();
}

cachedWebPage = webBrowser.DocumentText;
this.webBrowser.Dispose();
}

private void UpdateInfo_FormClosed(object sender, FormClosedEventArgs e) {
log.Info("Closed. " + e.CloseReason.ToString());
this.optionChosen = DialogResult.Cancel;
}

private void UpdateInfo_FormClosing(object sender, FormClosingEventArgs e) {
log.Info("Closing. " + e.CloseReason.ToString());
}
#endregion
}
}
29 changes: 23 additions & 6 deletions src/OutlookGoogleCalendarSync/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public static Boolean IsSquirrelInstall() {
private async Task<Boolean> githubCheck() {
log.Debug("Checking for Squirrel update...");
UpdateManager updateManager = null;
Forms.UpdateInfo updateInfoFrm = null;
UpdateInfo updates = null;
isBusy = true;
try {
String installRootDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
Expand All @@ -108,7 +110,15 @@ private async Task<Boolean> githubCheck() {
else
updateManager = new Squirrel.UpdateManager(nonGitHubReleaseUri, "OutlookGoogleCalendarSync", installRootDir);

UpdateInfo updates = await updateManager.CheckForUpdate();
try {
updates = await updateManager.CheckForUpdate();
} catch (System.Exception ex) {
if (ex.Message.Contains("Couldn't acquire lock")) {
log.Fail(ex.Message);
return false;
}
throw;
}
if ((Settings.Instance.AlphaReleases && updates.ReleasesToApply.Any()) ||
updates.ReleasesToApply.Any(r => r.Version.SpecialVersion != "alpha")) {

Expand Down Expand Up @@ -173,10 +183,10 @@ private async Task<Boolean> githubCheck() {
}
}

var t = new System.Threading.Thread(() => new Forms.UpdateInfo(releaseVersion, releaseType, releaseNotes, out dr));
var t = new System.Threading.Thread(() => updateInfoFrm = new Forms.UpdateInfo(releaseVersion, releaseType, releaseNotes, out dr));
t.SetApartmentState(System.Threading.ApartmentState.STA);
t.Start();
t.Join();
t.Join();

squirrelGaEv = new(Telemetry.GA4Event.Event.Name.squirrel);
squirrelGaEv.AddParameter(GA4.Squirrel.state, "Upgrade pending");
Expand All @@ -198,12 +208,13 @@ private async Task<Boolean> githubCheck() {
squirrelGaEv.AddParameter(GA4.Squirrel.action_taken, "Upgrade");

log.Info("Applying the updated release(s)...");
updateInfoFrm.PrepareForUpgrade();
//updateManager.UpdateApp().Wait();

int ApplyAttempt = 1;
while (ApplyAttempt <= 5) {
try {
updateManager.ApplyReleases(updates).Wait();
await updateManager.ApplyReleases(updates, updateInfoFrm.ShowUpgradeProgress);
break;
} catch (System.AggregateException ex) {
ApplyAttempt++;
Expand All @@ -226,8 +237,13 @@ private async Task<Boolean> githubCheck() {

log.Info("The application has been successfully updated.");
squirrelGaEv.AddParameter(GA4.Squirrel.result, "Successful");
OgcsMessageBox.Show("The application has been updated and will now restart.",
"OGCS successfully updated!", MessageBoxButtons.OK, MessageBoxIcon.Information);

updateInfoFrm.UpgradeCompleted();
while (updateInfoFrm.AwaitingRestart) {
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}

restartUpdateExe = updateManager.RootAppDirectory + "\\Update.exe";
return true;

Expand Down Expand Up @@ -283,6 +299,7 @@ private async Task<Boolean> githubCheck() {
throw;
} finally {
isBusy = false;
updateInfoFrm?.Dispose();
updateManager.Dispose();
}
return false;
Expand Down

0 comments on commit 6eced8f

Please sign in to comment.