Read StandardOutput Process from BackgroundWorkerProcess method

Posted by Nicola Celiento on Stack Overflow See other posts from Stack Overflow or by Nicola Celiento
Published on 2010-06-18T14:10:09Z Indexed on 2010/06/18 14:13 UTC
Read the original article Hit count: 409

Hi all,

I'm working with a BackgroundWorker Process from Windows .NET C# application.

From the async method I call many instance of cmd.exe Process (foreach), and read from StandardOutput, but at the 3-times cycle the ReadToEnd() method throw Timeout Exception.

Here the code:

    StreamWriter sw = null;
DataTable dt2 = null;
StringBuilder result = null;
Process p = null;

for(DataRow dr in dt1.Rows)
{
    arrLine = new string[4];    //new row
    result = new StringBuilder();

    arrLine[0] = dr["ID"].ToString();
    arrLine[1] = dr["ChangeSet"].ToString();

    #region Initialize Process CMD
    p = new Process();
    ProcessStartInfo info = new ProcessStartInfo();
    info.FileName = "cmd.exe";
    info.CreateNoWindow = true;
    info.RedirectStandardInput = true;
    info.RedirectStandardOutput = true;
    info.RedirectStandardError = true;
    info.UseShellExecute = false;
    p.StartInfo = info;
    p.Start();
    sw = new StreamWriter(p.StandardInput.BaseStream);
    #endregion

    using (sw)
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine("cd " + this.path_WORKDIR);
            sw.WriteLine("tfpt getcs /changeset:" + arrLine[1]);
        }
    }

    string strError = p.StandardError.ReadToEnd();      // Read shell error output commmand (TIMEOUT)
    result.Append(p.StandardOutput.ReadToEnd());        // Read shell output commmand

    sw.Close();
    sw.Dispose();

    p.Close();
    p.Dispose();
} 

Timeout is on code line:

string strError = p.StandardError.ReadToEnd();

Can you help me?

Thanks!

© Stack Overflow or respective owner

Related posts about process

Related posts about backgroundworker