471,595 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

External Process will not end correctly

Hi Guys,

I am trying to caputre the output for an external application. The idea is
to use the System.Diagnostics.Process to run the exe in a process and
redirect the output to a string.

When I run the external command it works fine. But when i wrap it in a .net
exe it fails to exit and the application does not exit. If I kill the
external exe from task manager, the .net wrap completes.

If I proc.StartInfo.UseShellExecute to true, i can get the external
application to run, but then i lose the output.

Here is a snippet of the code.

static void Main(string[] args)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName=@"C:\Program.exe";
proc.StartInfo.Arguments="";
proc.Start();
proc.WaitForExit();
string stdoutput = proc.StandardOutput.ReadToEnd();
string stderror = proc.StandardError.ReadToEnd();
Console.WriteLine (stdoutput);
Console.WriteLine (stderror);
}

Cheers

Craig.
Jan 9 '07 #1
1 2172
Hi guys,

Got it working, just had to move the proc.WaitForExit(); line to below the
read lines.

Cheers

Craig.

"Craig" wrote:
Hi Guys,

I am trying to caputre the output for an external application. The idea is
to use the System.Diagnostics.Process to run the exe in a process and
redirect the output to a string.

When I run the external command it works fine. But when i wrap it in a .net
exe it fails to exit and the application does not exit. If I kill the
external exe from task manager, the .net wrap completes.

If I proc.StartInfo.UseShellExecute to true, i can get the external
application to run, but then i lose the output.

Here is a snippet of the code.

static void Main(string[] args)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName=@"C:\Program.exe";
proc.StartInfo.Arguments="";
proc.Start();
proc.WaitForExit();
string stdoutput = proc.StandardOutput.ReadToEnd();
string stderror = proc.StandardError.ReadToEnd();
Console.WriteLine (stdoutput);
Console.WriteLine (stderror);
}

Cheers

Craig.
Jan 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bruce Arp | last post: by
4 posts views Thread by My SQL | last post: by
1 post views Thread by Marcel | last post: by
1 post views Thread by david.sargent | last post: by
3 posts views Thread by lookaround | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.