launch exe with params, but program closes instantly after opening?

此生再无相见时 提交于 2020-01-16 03:29:27

问题


I am writing a client for my gaming community and one of the functions of this client is to launch a game via the client with parameters that will enable our community mod pack on launch.

When I press the button, the game begins to launch and as soon as the program opens (the icon pops up in the task bar), it closes instantly.

Is there something I am missing that is needed to keep the launched exe running?

Here is my code:

private void btnLaunchGame_Click(object sender, EventArgs e)
    {
        string armaPath = gameDir+"/Expansion/beta/";
        string filename = Path.Combine(armaPath, "arma2oa.exe");
        string launchParams = "-noSplash -noFilePatching -showScriptErrors \"-name=Meta\" \"-mod=I:/Steam/steamapps/common/Arma 2;expansion;expansion/beta;expansion/beta/expansion;servermods/@HC_DAYZ;servermods/@HC_WEAPONS;servermods/@HC_EXTRAS;servermods/@HC_ACE\"";
        System.Diagnostics.Process.Start(filename, launchParams);
    }//close Game Launch

Any ideas is appreciated!

I have a .bat file that will execute the game flawlessly with the launch args listed below, this could possibly help pinpoint the cause of my problem: http://puu.sh/5CGKk.png (couldn't get code to paste in a readable format).


回答1:


Try using Process:

        Process process = new Process();
        process.StartInfo.FileName = "arma2oa.exe";
        process.StartInfo.Arguments = "-noSplash -noFilePatching -showScriptErrors \"-name=Meta\" \"-mod=I:/Steam/steamapps/common/Arma 2;expansion;expansion/beta;expansion/beta/expansion;servermods/@HC_DAYZ;servermods/@HC_WEAPONS;servermods/@HC_EXTRAS;servermods/@HC_ACE\"";
        process.StartInfo.WorkingDirectory = gameDir + "/Expansion/beta/";
        process.Start();

It may be what exe require working directory to be set. Or it will crash, unable to load resources.

If that doesn't works, then perhaps you need to add

            process.WaitForInputIdle();

before exiting function running process. I don't know why, but running Acrobat Reader without this wait may sometimes cause a wierd effect: Acrobat is running, but the document, passed via arguments, is not shown. Perhaps something to do with Garbage collector or Process itself.




回答2:


Try following

using (Process process = new Process())
{
   ProcessStartInfo startInfo = new ProcessStartInfo("C:\Program Files\Arma2oa\Arma2oa.exe");
   startInfo.Arguments = "-noSplash -noFilePatching -showScriptErrors \"-name=Meta\" \"-mod=I:/Steam/steamapps/common/Arma 2;expansion;expansion/beta;expansion/beta/expansion;servermods/@HC_DAYZ;servermods/@HC_WEAPONS;servermods/@HC_EXTRAS;servermods/@HC_ACE\"";
   process.StartInfo = startInfo;
   process.Start();
}


来源:https://stackoverflow.com/questions/20392489/launch-exe-with-params-but-program-closes-instantly-after-opening

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!