-
April 14th, 2011, 12:17 PM
#1
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;
proc.Start();
return proc.StandardOutput.ReadToEnd();
}
-
April 14th, 2011, 12:54 PM
#2
Re: Execute Command script sometimes goes not responding
I found that it was crashing while trying to return the output. Since I did not need output for this specific function I just made a new void function called ExecuteCommandNoReturn. Strange indeed.
-
April 14th, 2011, 02:02 PM
#3
Re: Execute Command script sometimes goes not responding
Originally Posted by DeepThought
I found that it was crashing while trying to return the output. Since I did not need output for this specific function I just made a new void function called ExecuteCommandNoReturn. Strange indeed.
It doesn't seem strange. After all, you are telling it to wait for output that never arrives.
Code:
return proc.StandardOutput.ReadToEnd();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|