Execute Command script sometimes goes not responding
I am using the following function I found on a blog and it works great for executing commands. However when I run a command that looks like this:
psexec \\someserver some command
My program goes into not responding, just never comes back. I have verified that this command in a console does return something immediately.
I thought about executing the command in another thread, but I don't want to leave threads open.
Here is the function:
public string ExecuteCommand(object command)
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;