运行 从 c# wpf 应用程序安装进程

Running install process from c# wpf application

在我的 C# WPF 应用程序的启动过程中,我正在检查是否需要安装新版本。如果是这样,我想中断当前进程并启动安装程序。安装程序是使用 NSIS 包开发的。 问题是有时只有来自 NSIS 安装程序的用户帐户控制对话框出现并且安装过程中断。

如何保证每次都执行安装过程?

这是我的应用程序启动代码。

protected override void OnStartup(StartupEventArgs e)
    {
        try
        {
            //Disable shutdown when the dialog closes
            Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

            if ( IfUpdateRequired())
            {
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(sessionCtx.AutoUpdateVersionInfo.SetupPath);
                //This should not block current program
                startInfo.UseShellExecute = true;  
                startInfo.Verb = "runas";
                System.Diagnostics.Process.Start(startInfo);
                this.Shutdown();
            }
            else
            {
                base.OnStartup(e);
            }
        }
        catch (Exception ex)
        {

        }
    }

我唯一的猜测是在您的进程退出之前子进程还没有完全启动。允许 ShellExecute 异步执行其操作。

如果这是原因,那么您应该可以通过在调用 this.Shutdown() 之前睡一会儿来解决它。也许等待 10 秒左右?或者在进程中调用 WaitForInputIdle(9999)。或者您可以查看响应过程 属性?