C# WPF程序在静默运行时未正确运行
我已经编写了一个小程序来在客户机上执行快速配置,它需要能够使用GUI运行,并且可以从命令行静默运行。如果我使用GUI运行它,那么它工作得很好,但是如果我尝试在没有GUI的情况下运行它,那么它就会挂起 我已将问题追溯到代码的这一部分:C# WPF程序在静默运行时未正确运行,c#,.net,wpf,C#,.net,Wpf,我已经编写了一个小程序来在客户机上执行快速配置,它需要能够使用GUI运行,并且可以从命令行静默运行。如果我使用GUI运行它,那么它工作得很好,但是如果我尝试在没有GUI的情况下运行它,那么它就会挂起 我已将问题追溯到代码的这一部分: string arg = "/C:\"setup.exe /qn ADD_OPINSIGHTS_WORKSPACE=1 OPINSIGHTS_WORKSPACE_ID=" + workSpaceID + " OPINSIGHTS_WORKSPACE_KEY=
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和静默代码在一个单独的类中,以完全相同的方式运行,但只有在静默运行时才能到达“Install started”。我知道exe确实完成了,因此我尝试在此解决方案中使用该代码,但遇到了相同的问题:
我也有同样的问题 我上了一堂创业课:
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
}
}
在此之后,您必须将应用程序启动对象更改为Startup类。我以异步方式运行所有工作,因为我没有加载GUI线程,所以Windows将应用程序视为控制台应用程序。然而,GUI线程会调用其他异步方法并等待它们完成,而控制台应用程序调用这些方法,然后关闭,因为它已无事可做。解决方案是显式地让主线程像这样等待:
public static void Main(string[] args)
{
try
{
Install().Wait();
}
catch (Exception ex)
{
}
}
private static async Task Install()
{}
你绝对确定exe不需要任何用户输入吗?是的,我也尝试过在没有任何参数的情况下运行它,并以同样的效果手动执行它。这是一个有趣的想法,但它仍然适用于GUI,并在静默模式下暂停:(