Execute multiple command lines with the same process using C#

十年热恋 提交于 2019-11-29 11:58:56

Your question is a little confusing but I think i see your problem. First you should check out this blog post to see common issues with System.Diagnostics.Process. Your code happens to violate one that isn't listed there. The reuse of the Process object itself.

You need to refactor the code like:

    class MyProcessStarter
    {
        private ProcessStartInfo _startInfo = new ProcessStartInfo();
        public MyProcessStarter(string exe, string workingDir)
        {
            _startInfo.WorkingDirectory = workingDir;
            _startInfo.FileName = exe;
            _startInfo.UseShellExecute = false;
            _startInfo.RedirectStandardOutput = true;
        }

        public string Run(string arguments)
        {
            _startInfo.Arguments = arguments;
            Process p = Process.Start(_startInfo);
            p.Start();
            string strOutput = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            return strOutput;
        }
    }

I've written a more complete and accurate implementation called ProcessRunner. The following demonstrates it's usage to perform the same operation:

using CSharpTest.Net.Processes;
partial class Program
{
    static int Main(string[] args)
    {

        ProcessRunner run = new ProcessRunner("svn.exe");
        run.OutputReceived += new ProcessOutputEventHandler(run_OutputReceived);
        return run.Run("update", "C:\\MyProject");
    }

    static void run_OutputReceived(object sender, ProcessOutputEventArgs args)
    {
        Console.WriteLine("{0}: {1}", args.Error ? "Error" : "Output", args.Data);
    }
}

You need to READ ALL data from input, before send another command!

And you can't ask to READ if no data is avaliable... little bit suck isn't?

My solutions... when ask to read... ask to read a big buffer... like 1 MEGA...

And you will need wait a min 100 milliseconds... sample code...

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oProcess As New Process()
        Dim oStartInfo As New ProcessStartInfo("cmd.exe", "")
        oStartInfo.UseShellExecute = False
        oStartInfo.RedirectStandardOutput = True
        oStartInfo.RedirectStandardInput = True
        oStartInfo.CreateNoWindow = True
        oProcess.StartInfo = oStartInfo
        oProcess.Start()

        oProcess.StandardInput.WriteLine("dir")


        Threading.Thread.Sleep(100)

        Dim Response As String = String.Empty

        Dim BuffSize As Integer = 1024 * 1024
        Dim bytesRead As Integer = 0

        Do

            Dim x As Char() = New Char(BuffSize - 1) {}
            bytesRead = oProcess.StandardOutput.Read(x, 0, BuffSize)

            Response = String.Concat(Response, String.Join("", x).Substring(0, bytesRead))            

        Loop While oProcess.StandardOutput.Peek >= 0



        MsgBox(Response)
        Response = String.Empty



        oProcess.StandardInput.WriteLine("dir c:\")




        Threading.Thread.Sleep(100)


        bytesRead = 0

        Do

            Dim x As Char() = New Char(BuffSize - 1) {}
            bytesRead = oProcess.StandardOutput.Read(x, 0, BuffSize)

            Response = String.Concat(Response, String.Join("", x).Substring(0, bytesRead))
            'Response = String.Concat(Response, String.Join("", x))

        Loop While oProcess.StandardOutput.Peek >= 0

        MsgBox(Response)


    End Sub
End Class
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!