简体   繁体   中英

Running install process from c# wpf application

In my C# WPF Application in statup process I'm checking whether the installation of new version is required. If so, I want to break current process and start installer. The installer is developed using NSIS package. The problem is that sometimes only User Account Control dialog from NSIS installer appears and install process breaks.

在此处输入图片说明

How to ensure that the installation process is executed every time?

Here is my code on Application Startup.

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)
        {

        }
    }

My only guess is that the child process has not fully started before your process quits. ShellExecute is allowed to perform its operation asynchronously.

If this is the cause then you should be able to work around it by sleeping a bit before calling this.Shutdown() . Wait 10 seconds or so perhaps? Or call WaitForInputIdle(9999) on the process. Or maybe you could check the Responding process property?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM