Alternative native api for Process.Start

Posted by Akash Kava on Stack Overflow See other posts from Stack Overflow or by Akash Kava
Published on 2010-04-13T18:07:51Z Indexed on 2010/04/13 18:23 UTC
Read the original article Hit count: 426

Filed under:
|
|

Ok this is not duplicate of "http://stackoverflow.com/questions/2065592/alternative-to-process-start" because my question is something different here.

I need to run a process and wait till execution of process and get the output of console.

There is way to set RedirectStandardOutput and RedirectStandardError to true, however this does not function well on some machines, (where .NET SDK is not installed), only .NET runtime is installed, now it works on some machines and doesnt work on some machines so we dont know where is the problem.

I have following code,

        ProcessStartInfo info = new ProcessStartInfo("myapp.exe", cmd);
        info.CreateNoWindow = true;
        info.UseShellExecute = false;
        info.RedirectStandardError = true;
        info.RedirectStandardOutput = true;
        Process p =  Process.Start(info);
        p.WaitForExit();
        Trace.WriteLine(p.StandardOutput.ReadToEnd());
        Trace.WriteLine(p.StandardError.ReadToEnd());

On some machines, this will hang forever on p.WaitForExit(), and one some machine it works correctly, the behaviour is so random and there is no clue.

Now if I can get a real good workaround for this using pinvoke, I will be very happy.

© Stack Overflow or respective owner

Related posts about pinvoke

Related posts about c#