与外部进程通过StdIn和StdOut进行通信时,Excel VBA挂起

我在VBA中创build了一个简单的ProgramExecutor类,以方便与控制台应用程序的通信。

Option Explicit Dim wshShell, shellExec Public Sub Run(dir As String, executable As String) Set wshShell = VBA.CreateObject("WScript.Shell") wshShell.CurrentDirectory = dir Set shellExec = wshShell.exec(executable) End Sub Public Sub SendCommand(command As String) oExec.StdIn.WriteLine command End Sub Public Function GetResponse() As String Dim sOutput As String While Not shellExec.stdOut.AtEndOfStream sOutput = sOutput & shellExec.stdOut.ReadLine If Not shellExec.StdErr.AtEndOfStream Then shellExec.StdErr.ReadLine End If Wend GetResponse = sOutput End Function Public Sub Terminate() shellExec.Terminate End Sub 

我试图与简单的应用程序进行通信,但每当我应用程序期待inputmacros挂断时,我调用shellExec.stdOut.AtEndOfStreamshellExec.stdOut.readAll

应用程序代码如下所示

 namespace SampleChat { class Program { static void Main(string[] args) { while (true) { var text = Console.ReadLine(); Console.WriteLine($"Echo {text}"); } } } } 

如何在应用程序将文本写入stdOut之后预期stdIn上的某些内容时如何读取整个stdOut?

示例用法:

 Public Sub StartChatting() Dim executor As ProgramExecutor Set executor = New ProgramExecutor executor.Run "C:\Projects\SampleChat\SampleChat\bin\Debug", "samplechat.exe" Dim messageToSend As String, sOutput As String Do While True messageToSend = InputBox("Type message") If messageToSend = vbNullString Then Exit Do End If executor.SendCommand messageToSend sOutput = executor.GetResponse() MsgBox (sOutput) Loop executor.Terminate End Sub