tags:

views:

55

answers:

2

Hello

I have a basic winform, calling an external program (SVN). I want to display the output produced by SVN in a textbox in the form.

Here is my code for calling this external program :

private void SVNcmd(string args, string workingcopy)
{
    textBoxOutput.Text += workingcopy + Environment.NewLine
        + args + Environment.NewLine;
    Process p = new Process();
    p.StartInfo.WorkingDirectory = workingcopy;
    p.StartInfo.FileName = "svn";
    p.StartInfo.Arguments = args;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false;
    p.Start();
    textBoxOutput.Text += p.StandardOutput.ReadToEnd() + Environment.NewLine;
    p.WaitForExit(); 
}

It's called inside a "foreach". The problem is, when I launch the command, I have to wait until each and every command has been finished... and it can take quite a while. During this time, the form freezes, and nothing is displayed in the textbox.
Maybe with this method the commands are launched at the same time, despite the WaitForExit ? I'm not sure, I'm not familiar with this type of problems (I'm more of a web developer).

What can I do to display the SVN output and stop the form from freezing while the program runs ?

Thanks

+1  A: 

Use a backgroundworker to do this. In the backgroundworder you can report progress and output to the UI.

Good example on MSDN

Let you worker DoWork do your code. Where you get read the lines you should do worker.ReportProgress. Register on that event and get the line from the param (the event).

PoweRoy
Thanks, but how do I do that ?
cosmo0
+1  A: 

You can use Process.BeginOutputReadLine() and BeginErrorReadLine() to keep your UI updated while the program is executing. There is a good example in the MSDN library topic for these methods. Beware that the callback runs on a thread, you have to use the form's BeginInvoke method to marshal a call to the UI thread and update the control. Use the Process.Exited event instead of WaitForExit() to detect completion of the program.

This approach also avoids a deadlock you'll get when the program is writing too much output to fit in the console output buffer, usually around 2K.

Hans Passant