vendredi 11 septembre 2015

Update C# form RichTextBox content (in realtime) with the output of the batch file execution without waiting for the batch command execution to finish

Below is my C# code in the form.

    private void callInfraCommand(string cmd)
    {
        string os_name = GetOSFriendlyName();
        var m_command = new System.Diagnostics.Process();

        // set up output redirection
        m_command.StartInfo.RedirectStandardOutput = true;
        m_command.StartInfo.RedirectStandardError = true;
        m_command.StartInfo.CreateNoWindow = true;
        m_command.StartInfo.UseShellExecute = false;
        m_command.StartInfo.CreateNoWindow = true;
        m_command.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        m_command.EnableRaisingEvents = true;

        // see below for output handler
        m_command.ErrorDataReceived += proc_DataReceived;
        m_command.OutputDataReceived += proc_DataReceived;

        if (os_name.Contains("Windows 7") || os_name.Contains("2008") || os_name.Contains("Windows 8") || os_name.Contains("2012"))
            m_command.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
        else
            m_command.StartInfo.FileName = @"C:\WINNT\system32\cmd.exe";

        m_command.StartInfo.Arguments = cmd;
        m_command.Start();

        m_command.BeginErrorReadLine();
        m_command.BeginOutputReadLine();
        m_command.WaitForExit();
    }

    private void proc_DataReceived(object sender, DataReceivedEventArgs e)
    {
        // output will be in string e.Data
        if (e.Data != null)
            this.BeginInvoke(new Action(() => deploy_result_txt.Text += (Environment.NewLine + e.Data)));
    }

I am passing the windows batch file to callInfraComamnd and it's block of code is working fine. But, my batch file is taking more than 5 minutes to finish and during that time, my form is freezing and after the batch process completes it displaying all the output of the batch file in chunk.

Ideally, i would like to see the progress (intermediate output of the batch file) in the TextBox while the batch process is running in the background.

Can anyone please advise how can I resolve it (i am using .NET 4) ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire