Open command prompt synchronously and show the output on a textbox
<p>im trying for days now to "capture" in real time the output of command prompt, the best thing i did so far is the following, it synchronously starts the cmd and asynchronously the reading of the output ( i couldnt figure any other way to accomplish that for real time). Thing is that commands in the app continue like normal instead of waiting the process on the cmd to finish. i.e. the messagebox pop ups before the cmd has finished with its operations. Thanks for every answer :)</p>

<pre><code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public bool progressbool = false;
        public string strOutput;
        public string pathforit = Directory.GetCurrentDirectory();
        public string line;
        System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

    public Form1()
    {
        InitializeComponent();
        commandline();


    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void commandline()
    {

        pProcess.StartInfo.FileName = "cmd.exe";
        pProcess.StartInfo.UseShellExecute = false;
        pProcess.StartInfo.RedirectStandardInput = true;
        pProcess.StartInfo.RedirectStandardOutput = true;
        pProcess.StartInfo.CreateNoWindow = true;
        pProcess.Exited += new EventHandler(myProcess_Exited);
        pProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_OutputDataReceived);
        pProcess.Start();
        pProcess.BeginOutputReadLine();
        pProcess.StandardInput.WriteLine("dir");


    }


    void process_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
    {
        this.AddText(e.Data);
    }
    delegate void AddTextCallback(string text);
    private void AddText(string text)
    {
        if (this.textBox1.InvokeRequired)
        {
            AddTextCallback d = new AddTextCallback(AddText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.textBox1.Text += text + Environment.NewLine;
            textBox1.SelectionStart = textBox1.Text.Length;
            textBox1.ScrollToCaret();
            textBox1.Refresh();
        }
    }
    private void myProcess_Exited(object sender, System.EventArgs e)
    {

        MessageBox.Show("The commands Operations have finished");
    }
}
</code></pre>
