Batch file 使用vb启动批处理文件并实时流式输出到变量

Batch file 使用vb启动批处理文件并实时流式输出到变量,batch-file,streaming,real-time,Batch File,Streaming,Real Time,(在我的程序中,我启动一个批处理文件,如下所示: Dim p1 as new process Dim psi As New ProcessStartInfo(appdata & "file.bat") psi.RedirectStandardError = True psi.RedirectStandardOutput = True psi.CreateNoWindow = True psi.WindowStyle = ProcessWindowStyle.Hidden psi.

(在我的程序中,我启动一个批处理文件,如下所示:

Dim p1 as new process    
Dim psi As New ProcessStartInfo(appdata & "file.bat")
psi.RedirectStandardError = True
psi.RedirectStandardOutput = True
psi.CreateNoWindow = True
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.UseShellExecute = False
p1 = Process.Start(psi)
此批处理文件大约每秒输出一行或多行新行。我的目标是实时读取这些行。我希望每0.5-1秒更新一次数据

p1.StandardOutput.ReadToEnd()
上述方法不起作用。到目前为止,我尝试的每个方法都会等待批处理文件完成,而这正是我不再需要这些信息的时刻。:p

一定有一些简单的东西我错过了,但我似乎找不到。)

编辑: 使用新技巧:

AddHandler p1.OutputDataReceived, AddressOf OutputHandler1
AddHandler p1.ErrorDataReceived, AddressOf Errorhandler1
miner1.BeginOutputReadLine()

Private Shared Sub OutputHandler1(ByVal sendingProcess As Object, ByVal outLine As DataReceivedEventArgs)
  If Not String.IsNullOrEmpty(outLine.Data) Then
    MsgBox(outLine.Data)
  End If
End Sub
errorHandler1的相同子组件


这适用于其他进程(实时),但不适用于我的进程。不幸的是:p

您对程序结构没有太多解释,但我认为您应该在另一个线程中启动批处理程序,然后继续在主线程(或相反线程)中读取,这样两个线程都不会阻塞