访问一个进程的标准化输出
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim p = New Process
p.StartInfo.FileName = "ping"
p.StartInfo.Arguments = "www.baidu.net"
' 必须禁用操作系统外壳程序
p.StartInfo.UseShellExecute = False
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardOutput = True
p.Start()
Dim output = p.StandardOutput.ReadToEnd()
If (String.IsNullOrEmpty(output) = False) Then
TextBox1.AppendText(output + vbCrLf)
End If
p.WaitForExit()
p.Close()
End Sub
在进程的waitForExit期间,主程序是被等待的。可以把进程放到后台运行,新建一个BackgroundWorker部件,在按钮按下时运行
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim p = New Process
p.StartInfo.FileName = "ping"
p.StartInfo.Arguments = "www.baidu.net"
' 必须禁用操作系统外壳程序
p.StartInfo.UseShellExecute = False
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardOutput = True
p.Start()
Dim output = p.StandardOutput.ReadToEnd()
If (String.IsNullOrEmpty(output) = False) Then
Me.Invoke(Sub() TextBox1.AppendText(output + vbCrLf))
End If
p.WaitForExit()
p.Close()
End Sub