WPF 程序在静默运行时无法正常运行



>我编写了一个小程序来在客户端计算机上执行快速配置,它需要能够使用 GUI 运行并从命令行静默运行。如果我使用 GUI 运行它,那么它可以完美运行,但是如果我尝试在没有的情况下运行它,那么它就会挂起。

我已经将问题追溯到这段代码:

    string arg = "/C:"setup.exe /qn ADD_OPINSIGHTS_WORKSPACE=1 OPINSIGHTS_WORKSPACE_ID=" + workSpaceID + " OPINSIGHTS_WORKSPACE_KEY=" + workSpaceKey + " AcceptEndUserLicenseAgreement=1"";
log.Info(arg);
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "MMASetup-AMD64.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = arg;
try
{
    log.Info("try entered");
    // Start the process with the info we specified.
    // Call WaitForExit and then the using statement will close.
    using (Process exeProcess = Process.Start(startInfo))
    {
        log.Info("Install started");
        exeProcess.WaitForExit(30000);
        log.Info("Install exit code: " + (exeProcess.ExitCode).ToString());
        return (exeProcess.ExitCode).ToString();
    }
}
catch (Exception e)
{
    log.Error("MMA install threw an error: ", e);
    return e.Message;
}

此方法与 GUI 和静默代码位于单独的类中,并以完全相同的方式运行,但仅在静默运行时达到"安装已启动"。我知道exe确实完成了,所以我尝试在此解决方案中使用代码,但遇到了同样的问题:ProcessStartInfo 挂在"WaitForExit"上?为什么?

我遇到了同样的问题。

我做了一个启动类:

  public partial class Startup {
    // WPF App
    private App _app;
    [STAThread]
    public static void Main(string[] args) {
      try {
        //Do what you need
        //Check the args
        //Start your setup silent
        //start the WPF App if need it
        this._app = new App();
        this._app.InitializeComponent();
        this._app.Run();
      } catch (Exception ex) {
        //Logging ex
      }
    }

之后,您必须将应用程序启动对象更改为启动类。

我正在异步运行我的所有工作,并且因为我没有加载 GUI 线程,Windows 将应用程序视为控制台应用程序。 而 GUI 线程将调用其他异步方法并等待它们完成控制台应用程序调用这些方法,然后关闭,因为它没有什么可做的。解决方案是显式地让主线程像这样等待:

public static void Main(string[] args)
    {
        try
        {
            Install().Wait();
        }
        catch (Exception ex)
        {
        }
    }
    private static async Task Install()
    {}

相关内容

最新更新