private void bgw_Completed(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { // Tidy up and exit SetupCommonEventHandler.TidyUpAndExit(Path.Combine(Path.GetPathRoot(Environment.SystemDirectory), "AutoGoogleMeet")); } // Update UI lblCurrentOperation.Text = "完成"; pgBar.Value = pgBar.Maximum; btnNext.Enabled = true; }
// Custom event handler for cancel button private void ButtonCancel_OnClick(object sender, EventArgs e) { var result = MessageBox.Show( "設定尚未完成,如你現在離開,程式將不會對你的電腦進行變更。你是否確定要離開?", "Auto Google Meet 設定精靈", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { SetupCommonEventHandler.TidyUpAndExit(Path.Combine(Path.GetPathRoot(Environment.SystemDirectory), "AutoGoogleMeet")); } }
private void bgw_DoWork(object sender, DoWorkEventArgs e) { var copyUtil = e.Argument as SetupFileCopier; var installDir = Path.Combine( Path.GetPathRoot(Environment.SystemDirectory), "AutoGoogleMeet"); // code for debug, uncomment to test cancel function Thread.Sleep(500); if (!Directory.Exists(installDir)) { copyUtil.CreateDir(installDir); } else { // Warning of deletion var dResults = MessageBox.Show( "一個Auto Google Meet已經在本機上完成設定,繼續進行會刪除原有的設定。如果你要更新到新版,請使用內置的更新工具。\n" + "你是否要繼續?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dResults == DialogResult.Yes) { // Delete all existing files var attempt = 0; while (attempt < 5) // Try for at most 5 times { if (e.Cancel) // Canceled { Application.Exit(); // Leave without tidying up } attempt += 1; try { copyUtil.UpdateUIManually("正在嘗試刪除舊版..."); Directory.Delete(installDir, true); } catch { // If delete failed, kill the running process then delete again try { foreach (var proc in Process.GetProcessesByName("AutoGoogleMeet")) { copyUtil.UpdateUIManually("失敗,正在嘗試停止舊版,然後重試..."); if (proc.Id == Process.GetCurrentProcess().Id) { continue; } proc.Kill(); // kill running process break; } } catch { // ignored } // fails for 5 times, end installation and exit if (attempt == 5) { MessageBox.Show("無法刪除舊版的Auto Google Meet,設定精靈將會結束。", "Auto Google Meet 設定精靈", MessageBoxButtons.OK, MessageBoxIcon.Error); // The application will not be able to clean up under this circumstance // Exit automatically Application.Exit(); } continue; } break; } copyUtil.CreateDir(installDir); } else { // Exit if the user cancel SetupCommonEventHandler.TidyUpAndExit(installDir); } } copyUtil.CopyAll(Application.StartupPath, installDir, new Random().Next(0, 5) * 200); // Register to launch with Windows & Configure WebBrowser control try { var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); key?.SetValue("AutoGoogleMeet", Path.Combine(installDir, "AutoGoogleMeet.exe")); key?.Close(); // the below code allows WebBrowser to render like a IE 11 var IEkey = Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true); IEkey?.SetValue(Path.GetFileName(Application.ExecutablePath), 11001, RegistryValueKind.DWord); IEkey?.Close(); } catch { // ignored } }