none
Process.StartInfo.RedirectStandardInput缓冲区溢出? RRS feed

  • 问题

  •       写了一个程序,内容是用Process类来完成以前bat完成的内容,由于输入的内容很长,有好几百行,现在程序只能正常运行前面的40多行,查看了下输入的字符数,大约在8000左右,怀疑可能是超过Process.StartInfo.RedirectStandardInput本身的缓冲区(猜想是默认的8192字节?)造成的,但是现在没找到解决方法,autoflush已经设置成true,每调用一行命令随后Myprocess.StandardInput.flush也是老样子.
          有什么方法解决吗?急~

          以下是测试用的代码,运行到一半就停止不动了,也没有任何报错,急于想知道真实原因.
    Imports System.IO
    
    Module Module1
    
        Sub Main()
    
            Dim aa(500) As String
    
            For i As Integer = 0 To 500
                aa(i) = "echo 12345678901234567890123456789012345678901234567890123456789012345678901234567890"
            Next
    
            EXECCMD(aa)
    
            Console.ReadKey()
    
        End Sub
    
        Private Sub EXECCMD(ByVal sCmd() As String)
            Dim MyProcess As New Process
    
            Dim b As Integer
    
    
            Try
                MyProcess.StartInfo.FileName = "cmd.exe"
    
                MyProcess.StartInfo.UseShellExecute = False
    
                MyProcess.StartInfo.RedirectStandardInput = True
    
                MyProcess.StartInfo.RedirectStandardOutput = True
    
                MyProcess.StartInfo.RedirectStandardError = True
    
                MyProcess.StartInfo.CreateNoWindow = True
    
                MyProcess.Start()
    
                For i As Integer = 0 To sCmd.GetUpperBound(0)
                    Console.WriteLine("sCmd(" & i & ")=" & sCmd(i)) 'TODO delete
                    b += sCmd(i).Length
                    MyProcess.StandardInput.WriteLine(sCmd(i))
                    Console.WriteLine("datalen=" & b)
                Next
    
                MyProcess.StandardInput.WriteLine("exit")
    
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub
    End Module
    
    2009年12月26日 15:10

答案