From 6eced8fb066431ad69278751e80281dd07a887ff Mon Sep 17 00:00:00 2001 From: Paul Woolcock <11843015+phw198@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:21:30 +0100 Subject: [PATCH] Provide upgrade progress indication. Resolve UI freeze. #1837 --- .../Forms/UpdateInfo.Designer.cs | 30 ++++-- .../Forms/UpdateInfo.cs | 95 ++++++++++++++++++- src/OutlookGoogleCalendarSync/Updater.cs | 29 ++++-- 3 files changed, 138 insertions(+), 16 deletions(-) diff --git a/src/OutlookGoogleCalendarSync/Forms/UpdateInfo.Designer.cs b/src/OutlookGoogleCalendarSync/Forms/UpdateInfo.Designer.cs index e60143da..37fd47c8 100644 --- a/src/OutlookGoogleCalendarSync/Forms/UpdateInfo.Designer.cs +++ b/src/OutlookGoogleCalendarSync/Forms/UpdateInfo.Designer.cs @@ -10,10 +10,19 @@ partial class UpdateInfo { /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { - if (disposing && (components != null)) { - components.Dispose(); + if (this.optionChosen == System.Windows.Forms.DialogResult.Yes && !this.AwaitingRestart) { + this.Visible = true; + disposing = false; + } + System.Windows.Forms.MessageBox.Show("Disposing of updateInfo form."); + try { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } catch { + //WebBrowser COM object is already disposed of, but we don't care at this point. } - base.Dispose(disposing); } #region Windows Form Designer generated code @@ -53,12 +62,13 @@ private void InitializeComponent() { this.btUpgrade.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btUpgrade.DialogResult = System.Windows.Forms.DialogResult.Yes; this.btUpgrade.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.btUpgrade.Location = new System.Drawing.Point(407, 260); + this.btUpgrade.Location = new System.Drawing.Point(396, 257); this.btUpgrade.Name = "btUpgrade"; - this.btUpgrade.Size = new System.Drawing.Size(75, 23); + this.btUpgrade.Size = new System.Drawing.Size(85, 30); this.btUpgrade.TabIndex = 1; this.btUpgrade.Text = "Upgrade"; this.btUpgrade.UseVisualStyleBackColor = true; + this.btUpgrade.Click += new System.EventHandler(this.btUpgrade_Click); // // wbPanel // @@ -92,9 +102,9 @@ private void InitializeComponent() { // this.btLater.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btLater.DialogResult = System.Windows.Forms.DialogResult.No; - this.btLater.Location = new System.Drawing.Point(326, 260); + this.btLater.Location = new System.Drawing.Point(305, 257); this.btLater.Name = "btLater"; - this.btLater.Size = new System.Drawing.Size(75, 23); + this.btLater.Size = new System.Drawing.Size(85, 30); this.btLater.TabIndex = 3; this.btLater.Text = "Later"; this.btLater.UseVisualStyleBackColor = true; @@ -132,9 +142,9 @@ private void InitializeComponent() { // this.btSkipVersion.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btSkipVersion.DialogResult = System.Windows.Forms.DialogResult.Ignore; - this.btSkipVersion.Location = new System.Drawing.Point(16, 260); + this.btSkipVersion.Location = new System.Drawing.Point(16, 257); this.btSkipVersion.Name = "btSkipVersion"; - this.btSkipVersion.Size = new System.Drawing.Size(104, 23); + this.btSkipVersion.Size = new System.Drawing.Size(104, 30); this.btSkipVersion.TabIndex = 7; this.btSkipVersion.Text = "Skip This Version"; this.btSkipVersion.UseVisualStyleBackColor = true; @@ -161,6 +171,8 @@ private void InitializeComponent() { this.Name = "UpdateInfo"; this.Text = "OGCS Update Available"; this.TopMost = true; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.UpdateInfo_FormClosing); + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.UpdateInfo_FormClosed); this.wbPanel.ResumeLayout(false); this.wbPanel.PerformLayout(); this.ResumeLayout(false); diff --git a/src/OutlookGoogleCalendarSync/Forms/UpdateInfo.cs b/src/OutlookGoogleCalendarSync/Forms/UpdateInfo.cs index 7a55017e..5fbe3a28 100644 --- a/src/OutlookGoogleCalendarSync/Forms/UpdateInfo.cs +++ b/src/OutlookGoogleCalendarSync/Forms/UpdateInfo.cs @@ -1,4 +1,5 @@ using System; +using System.Drawing; using System.Windows.Forms; using log4net; @@ -8,6 +9,7 @@ public partial class UpdateInfo : Form { private String version = ""; private String anchorRequested = ""; + private DialogResult optionChosen = DialogResult.None; private String htmlHead = @"
@@ -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 { @@ -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; } } @@ -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 } } diff --git a/src/OutlookGoogleCalendarSync/Updater.cs b/src/OutlookGoogleCalendarSync/Updater.cs index d13e0bed..234e65f6 100644 --- a/src/OutlookGoogleCalendarSync/Updater.cs +++ b/src/OutlookGoogleCalendarSync/Updater.cs @@ -99,6 +99,8 @@ public static Boolean IsSquirrelInstall() { private async Task