本文介绍了读取标准输出并检查多个进程的状态VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行多个命令行进程,以循环方式启动它们.它可以正常工作并启动所有异步操作.

I run multiple command line processes, starting them in a loop. It works and starts all of them async.

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并检查其状态吗?我需要等到他们完成后才能继续执行程序.

Haw can I read stdOutput for all of them and check their status?I need to wait until they finish to continue executing the program.

推荐答案

我建议您在循环内为每个进程分配 async 事件,以将其触发当该进程有任何输出时.标准输出或错误输出:

I would recommend you to assign async events to every process inside a loop, so they get fired when that process has any output. Standard Output or Error Output:

 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()

异步事件应该是这样的:

the async events should be something like this:

 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

这篇关于读取标准输出并检查多个进程的状态VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:42