Home > Software design >  c# start a exe until it is completely started and then append the arguments
c# start a exe until it is completely started and then append the arguments

Time:06-30

I was trying to start a exe with arguments by Process.Start. My first try is using Process.Start("Path/of/the/exe", "arguments of exe"). Here's my code snippets:

Process.Start(@"D:\Program Files\ITASCA\UDEC700\Exe64\udecConsole2017.exe", @"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");

However the initialization of this exe is a bit slow, and the result is, I can only start the exe but the failed passing arguments. The following is the screenshot: enter image description here which is exactly the same result that starts without arguments.

By referencing this post enter image description here SO I think the input arguments should be OK?

======================================= new addition 2:

code for checking outputstream end enter image description here

CodePudding user response:

It appears this is a console application and you are typing in the console after it starts. This typing is not arguments: Arguments are provided only when starting a new process and never change.

What you are doing is providing something to the standard input of the program. Console programs have three streams the OS provides (one input and two output). You need to redirect these to detect when the program has started and to provide the proper input.

Something like this:

// Start with stdio redirected
var psi = new ProcessStartInfo()
{
    UseShellExecute = false,
    FileName = @"your exe",
    RedirectStandardInput = true, 
    RedirectStandardOutput = true,
};
var p = System.Diagnostics.Process.Start(psi);

// Read until the udec> prompt
while(true)
{
    var line = p.StandardOutput.ReadLine();
    if(line.StartsWith("udec>"))
        break;
}

// Write the command
p.StandardInput.WriteLine(@"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");

// Read the result
p.StandardOutput.ReadToEnd();
  • Related