Mercurial CLI is slow in C#?

Posted by pATCheS on Stack Overflow See other posts from Stack Overflow or by pATCheS
Published on 2010-12-29T22:51:29Z Indexed on 2010/12/29 22:54 UTC
Read the original article Hit count: 264

Filed under:
|

I'm writing a utility in C# that will make managing multiple Mercurial repositories easier for the way my team is using it. However, it seems that there is always about a 300 to 400 millisecond delay before I get anything back from hg.exe. I'm using the code below to run hg.exe and hgtk.exe (TortoiseHg's GUI). The code currently includes a Stopwatch and some variables for timing purposes. The delay is roughly the same on multiple runs within the same session. I have also tried specifying the exact path of hg.exe, and got the same result.

static string RunCommand(string executable, string path, string arguments)
{
    var psi = new ProcessStartInfo()
    {
        FileName = executable,
        Arguments = arguments,
        WorkingDirectory = path,

        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,

        WindowStyle = ProcessWindowStyle.Maximized,
        CreateNoWindow = true
    };

    var sbOut = new StringBuilder();
    var sbErr = new StringBuilder();

    var sw = new Stopwatch();

    sw.Start();

    var process = Process.Start(psi);

    TimeSpan firstRead = TimeSpan.Zero;

    process.OutputDataReceived += (s, e) =>
    {
        if (firstRead == TimeSpan.Zero)
        {
            firstRead = sw.Elapsed;
        }

        sbOut.Append(e.Data);
    };
    process.ErrorDataReceived += (s, e) => sbErr.Append(e.Data);

    process.BeginOutputReadLine();
    process.BeginErrorReadLine();

    var eventsStarted = sw.Elapsed;

    process.WaitForExit();

    var processExited = sw.Elapsed;

    sw.Reset();

    if (process.ExitCode != 0 || sbErr.Length > 0)
    {
        Error.Mercurial(process.ExitCode, sbOut.ToString(), sbErr.ToString());
    }

    return sbOut.ToString();
}

Any ideas on how I can speed things up? As it is, I'm going to have to do a lot of caching in addition to threading to keep the UI snappy.

© Stack Overflow or respective owner

Related posts about c#

Related posts about mercurial