tags:

views:

178

answers:

5
A: 

I think you need to move

StreamReader output = process.StandardOutput;
string text = output.ReadToEnd();

after WaitToExit()

You may also want to redirect standard error in case something bad happens, you probably want to know about it.

Also you can shorten your code by doing string text= process.StandardOutput.ReadToEnd();

Ryu
Good suggestion but I'm afraid that didn't fix it!
Ed Woodcock
I'm aware of that, I was originally reading the output line by line, and appending a break at the end of each line so it was human-readable, I just switched it over to use ReadToEnd() to check that wasn't causing the problem!
Ed Woodcock
A: 

You will need to periodically read from the standard output pipe of the process while it is running. If you don't do this, then the standard output buffer will fill up and Windows will suspend the process and wait until it's clear before continuing. Of course, your process is sitting in a WaitForExit() so you have a deadlock.

This is a generic answer because I'm not familiar enough with the .NET process management primitives to give an example. However, the principle is the same as in any other piped output system.

Greg Hewgill
It only runs for about 1/2 a second and outputs a single line, I'm not sure that can fill the buffer! A suggestion of how to change the code might help?
Ed Woodcock
+2  A: 

Not quite an answer to your original question, but a different approach might be to use SharpSvn (http://sharpsvn.open.collab.net). By giving you more direct access to the API, it might give you better control and results.

I've used it to monitor and update svn work areas and it seemed to get the job done.

Rusty
Sounds like a good idea, but I'd rather not have to install anything else on our Dev server, it already spends a lot of time dying horribly!
Ed Woodcock
A: 

First I would recomend Rusty's suggestion. Secondly you can review this code for a working example of capturing process output.

If you just want to use the wrapper class from the link here is what you need:

using CSharpTest.Net.Processes;
 static void Update(string sourcePath, Action<string> output)
 {
  ProcessRunner run = new ProcessRunner("svn.exe", "update", "{0}");
  run.OutputReceived +=
   delegate(Object o, ProcessOutputEventArgs e) { output(e.Data); };
  int exitCode = run.RunFormatArgs(sourcePath);
  if (exitCode != 0)
   throw new ApplicationException(
    String.Format("SVN.exe returned {0}.", exitCode)
    );
 }
csharptest.net
+1  A: 

Here is some code taken from one of my apps - its basically just the MSDN sample. (http://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived.aspx)

private void SvnOutputHandler(object sendingProcess,
                                      DataReceivedEventArgs outLine)
{
    Process p = sendingProcess as Process;

    // Save the output lines here
}


private void RunSVNCommand()
{
    ProcessStartInfo psi = new ProcessStartInfo("svn.exe",
                                                string.Format("update \"{0}\" {1}", parm1, parm2));

    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;

    // Redirect the standard output of the sort command.  
    // This stream is read asynchronously using an event handler.
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;

    Process p = new Process();

    // Set our event handler to asynchronously read the sort output.
    p.OutputDataReceived += SvnOutputHandler;
    p.ErrorDataReceived += SvnOutputHandler;
    p.StartInfo = psi;

    p.Start();

    p.BeginOutputReadLine();
    p.BeginErrorReadLine();

    p.WaitForExit()
}
sylvanaar
WaitForEnd() is not a method on Process. Secondly you will not be certain of reading all output with this code. You MUST wait until both the OutputDataRecieved and ErrorDataRecieved events are fired with a null string.
csharptest.net
Ok, WaitForExit(), i'll fix it
sylvanaar
Great stuff, it's now capturing the error output too: I couldn't get it to do that synchronously!
Ed Woodcock