本地程序更新

var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
        Task.Run(async () =>
        {
            using (var updateManager = new UpdateManager(Model.CommonConstants.UpdateUrl))
            {
                var ignore = false;
                Exception lastError = null;
                while (true)
                {
                    try
                    {
                        lastError = null;
                        var updateInfo = await updateManager.CheckForUpdate(ignore);
                        if (updateInfo?.ReleasesToApply?.Any() == true)
                        {
                            var releases = updateInfo.ReleasesToApply;
                            SquirrelAwareApp.HandleEvents(
                              onInitialInstall: v =>
                              {
                                  updateManager.CreateShortcutForThisExe();
                                  //updateManager.CreateRunAtWindowsStartupRegistry();
                              },
                              onAppUninstall: v =>
                              {
                                  updateManager.RemoveShortcutForThisExe();
                                  //updateManager.RemoveRunAtWindowsStartupRegistry();
                              });
                            CommonConstants.CurrVersion = releases.First().Version?.ToString();
                            CommonConstants.PreVersion = updateInfo.CurrentlyInstalledVersion?.Version?.ToString();
                            ppMessage.BeginInvoke(new Action(() =>
                            {
                                ppMessage.Caption = "正在下载更新,请稍候...";
                            }));
                            await updateManager.DownloadReleases(releases, p => pbProgress.BeginInvoke(new Action(() =>
                            {
                                pbProgress.Position = p;
                            }))).ConfigureAwait(false);
                            ppMessage.BeginInvoke(new Action(() =>
                            {
                                ppMessage.Caption = "正在处理更新文件,请稍候...";
                            }));
                            await updateManager.ApplyReleases(updateInfo, p => pbProgress.BeginInvoke(new Action(() =>
                            {
                                pbProgress.Position = p;
                            }))).ConfigureAwait(false);
                            await updateManager.CreateUninstallerRegistryEntry();
                        }
                        break;
                    }
                    catch (Exception ex)
                    {
                        LogUtils.Exception(ex);
                        lastError = ex;
                        if (!ignore)
                        {
                            ignore = true;
                            continue;
                        }
                        break;
                    }
                }
                if (lastError != null)
                {
                    throw lastError;
                }
            }
        })
        .ContinueWith(async task =>
        {
            if (task.Exception != null)
            {
                XtraMessageBox.Show("执行更新时发生异常,请联系实施人员协助解决。",
                    "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
                LogUtils.Error($"升级异常{task.Exception}");
                task.Exception.Handle(_ => true);
            }
            else
            {
                Close();
                ConfigUtils.Main.ISUPDATE = true;
                ConfigUtils.Main.VERSION = CommonConstants.CurrVersion;
                ConfigUtils.Main.Save();
                BLL.GlobalDataBLL.InsertUserLogin(true);
                LogUtils.Information("等待GC完成");
                GC.WaitForFullGCComplete();
                LogUtils.Information("退出Application");
                //Environment.Exit(Environment.ExitCode);
                Application.Exit();
                LogUtils.Information("升级完成,重启Application");
                //await Task.Delay(2000);
                await UpdateManager.RestartAppWhenExited();
            }
        }, scheduler);
posted @ 2024-01-16 16:41  泽哥的学习笔记  阅读(16)  评论(0编辑  收藏  举报