[Mono-list] RE: executing a tool from within c#

Jacek Blaszczynski jacek.blaszczynski at acn.waw.pl
Mon Mar 13 08:11:25 EST 2006


Hello!

This a well known issue with several solutions posted around many
newsgroups. In general the way around it is to read StdError and StdOut
streams on separate threads and keep tool running on a 3rd thread or main
app thread. However never tested this on Mono, since my code uses .NET
specific stuff.

Hope this helps.

Cheers

-----Original Message-----
From: mono-list-bounces at lists.ximian.com
[mailto:mono-list-bounces at lists.ximian.com] On Behalf Of Martin Dvořák
Sent: Monday, March 13, 2006 12:36 PM
To: mono-list at lists.ximian.com
Subject: [Mono-list] RE: executing a tool from within c#

Would it help if the StandardError output was read from another thread?

Martin

> Malte,
>
> I use something along the lines of what is below.
>
> However, I pretty sure that execute_capture_output can cause deadlocks 
> within the running application.  This is due to the ordering of the lines:
>
>         string output = p.StandardError.ReadToEnd();
>         output += p.StandardOutput.ReadToEnd();
>
> The application can deadlock because it is blocked writting to its' 
> stdout which is not read from until its' stderr is closed (which will 
> never happen because it is blocked).
>
> Although this is a known issue I'm not sure there is a way around it 
> and no-one has suggested a solution.  Anyhow, this works for the most
part.
>
> Colin
>
> ---
>
> using System.Diagnostics;
> string output = p.StandardError.ReadToEnd();
>         output += p.StandardOutput.ReadToEnd(); namespace colib {
>   public class process_t {
>     public static string execute_capture_output(string cmd_line,
>                                                 out int exit_code) {
>       using (Process p = new Process()) {
>         int i = cmd_line.IndexOf(' ');
>         if (i == -1) {
>           p.StartInfo.FileName = cmd_line;
>         } else {
>           p.StartInfo.FileName = l_t.left(cmd_line, i);
>           p.StartInfo.Arguments = l_t.right(cmd_line, -i-1);
>         }
>
>         p.StartInfo.UseShellExecute = false;
>         p.StartInfo.RedirectStandardOutput = true;
>         p.StartInfo.RedirectStandardError = true;
>         p.Start();
>         string output = p.StandardError.ReadToEnd();
>         output += p.StandardOutput.ReadToEnd();
>
>         p.WaitForExit();
>         exit_code = p.ExitCode;
>
>         return output;
>       }
>     }
>
>     public static int execute(string path) {
>       Process p = new Process();
>       int i = path.IndexOf(' ');
>       if (i == -1) {
>         log_t.info(path);
>         p.StartInfo.FileName = path;
>       } else {
>         p.StartInfo.FileName = l_t.left(path, i);
>         p.StartInfo.Arguments = l_t.right(path, -i-1);
>       }
>
>       p.StartInfo.UseShellExecute = false;
>       p.Start();
>       p.WaitForExit();
>       int exit_code = p.ExitCode;
>       p.Close();
>       return exit_code;
>     }
>   }
> }
_______________________________________________
Mono-list maillist  -  Mono-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list




More information about the Mono-list mailing list