CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    27

    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();

    }

  2. #2
    Join Date
    Apr 2011
    Posts
    27

    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.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Execute Command script sometimes goes not responding

    Quote Originally Posted by DeepThought View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured