It seems that the output is not delivered until after the process has exited. I have declared the process at a global level and implemented a timer to check for output after the process has exited and it seems to work!
Here is the code for reference (The timer interval is set to 10 (ms) so Iwait up to 5 seconds for the output):
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace SCX_Server_Utilisation_Test_App
{
public partial class frmMain : Form
{
StringBuilder output;
StringBuilder error;
ProcessStartInfo psi;
Process proc;
int timerExecutions;
Stopwatch outputDelay;
public frmMain()
{
InitializeComponent();
}
private void btnGo_Click(object sender, EventArgs e)
{
try
{
btnGo.Enabled = false;
Stopwatch ExecutionStopWatch;
output = new StringBuilder();
error = new StringBuilder();
txtStandardOutput.Clear();
txtErrorOutput.Clear();
Application.DoEvents();
psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.FileName = txtPsExecPath.Text;
psi.Arguments = txtArguments.Text;
Trace.WriteLine(string.Format("Arguments: {0}",txtArguments.Text));
proc = new Process();
proc.EnableRaisingEvents = true;
proc.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived);
proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
proc.StartInfo = psi;
ExecutionStopWatch = new Stopwatch();
ExecutionStopWatch.Start();
Trace.WriteLine(string.Format("Execute PsExec command on server {0}.", cboServer.SelectedItem.ToString()));
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
Trace.WriteLine(string.Format("Execution took {0} ms", ExecutionStopWatch.ElapsedMilliseconds.ToString()));
outputDelay = new Stopwatch();
outputDelay.Start();
timerWaitForOutput.Enabled = true;
}
catch (Exception ex)
{
Trace.WriteLine("Exception occured: " + ex.ToString());
}
}
private void proc_OutputDataReceived(object sendingProcess, DataReceivedEventArgs message)
{
try
{
#region Try
if (!String.IsNullOrEmpty(message.Data))
{
output.AppendLine(message.Data);
}
#endregion Try
}
catch (Exception ex)
{
Trace.WriteLine("Exception occured: " + ex.ToString());
}
}
private void proc_ErrorDataReceived(object sendingProcess, DataReceivedEventArgs message)
{
try
{
#region Try
error.AppendLine(message.Data);
#endregion Try
}
catch (Exception ex)
{
Trace.WriteLine("Exception occured: " + ex.ToString());
}
}
private void frmMain_Load(object sender, EventArgs e)
{
cboServer.SelectedIndex = 1;
}
private void cboServer_SelectedIndexChanged(object sender, EventArgs e)
{
txtArguments.Text = string.Format("\\\\{0}.<domain name=""> \"{1}\" {2}", cboServer.SelectedItem.ToString(), txtRemoteExePath.Text);
}
private void timerWaitForOutput_Tick(object sender, EventArgs e)
{
timerExecutions++;
if (!output.ToString().Length.Equals(0) || timerExecutions >= 500)
{
proc.Close();
timerExecutions = 0;
timerWaitForOutput.Enabled = false;
outputDelay.Stop();
if (output.ToString().Length.Equals(0))
{
Trace.WriteLine(string.Format("Waited {0} ms for output with no results.", outputDelay.ElapsedMilliseconds.ToString()));
txtStandardOutput.Text = string.Format("Waited {0} ms for output with no results.", outputDelay.ElapsedMilliseconds.ToString());
}
else
{
Trace.WriteLine(string.Format("Output was delayed by {0} ms.", outputDelay.ElapsedMilliseconds.ToString()));
}
btnGo.Enabled = true;
}
if (!output.ToString().Length.Equals(0))
{
txtStandardOutput.Text = output.ToString();
}
if (!error.ToString().Length.Equals(0))
{
txtErrorOutput.Text = error.ToString();
}
}
}
}
</domain>