1

我运行多个命令行进程,循环启动它们。它可以工作并异步启动所有这些。

Public Sub DoWork
Dim i As Integer = 0
        While (Args_reader.Peek() > -1)
            i = i + 1
            MyArg = Args_reader.ReadLine
            Dim MyArg As String
            Dim MyProcess(i) As Process
            MyProcess(i) = New Process
            With MyProcess(i).StartInfo
                .FileName = MyFile
                .Arguments = MyArg
                .UseShellExecute = False
                .CreateNoWindow = True
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .RedirectStandardError = True
                .WindowStyle = ProcessWindowStyle.Hidden
            End With
            MyProcess(i).Start()
        End While
        Args_reader.Close()
        i = 0
End Sub

我怎么能读取它们的 stdOutput 并检查它们的状态?我需要等到他们完成才能继续执行程序。

4

1 回答 1

0

我建议您将async事件分配给every process循环内部,以便在该进程有任何输出时触发它们。标准输出或错误输出:

 Dim Proceso As New Process

 'Add the event handlers:

 AddHandler Proceso.OutputDataReceived, AddressOf CallbackProcesoAsync
 AddHandler Proceso.ErrorDataReceived, AddressOf ErrorDataReceivedAsync

 Dim startInfo As New ProcessStartInfo
 startInfo.FileName = execFile
 startInfo.RedirectStandardOutput = True
 startInfo.RedirectStandardError = True
 startInfo.CreateNoWindow = False
 Proceso.StartInfo = startInfo
 Proceso.Start()

异步事件应该是这样的:

 Private Sub CallbackProcesoAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)

    If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then

       'args.Data have the output

    End If

End Sub

Private Sub ErrorDataReceivedAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)

   If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then

       'args.Data have the output

   End If


End Sub
于 2013-10-22T09:41:23.207 回答