Hallo.
Ich möchte mehrere Prozesse starten und deren Standardoutput auswerten.
Dies tue ich folgendermaßen:
Bietet sich da eine Möglichkeit, dies mit Threads aus dem Threadpool zu realisieren?
Achso: Timeouterkennung funktioniert übrigens auch nicht.
Wenn ja, wie?
Ich möchte mehrere Prozesse starten und deren Standardoutput auswerten.
Dies tue ich folgendermaßen:
VB.NET-Quellcode
- Private Sub DoStart(StartPort As Integer, Ports As Integer, Timeout As Integer)
- Dim Watch As New Stopwatch
- Watch.Start()
- Dim AllInstances As New List(Of Thread)
- If Ports = 1 Then
- W(String.Concat(">> Starting ", CStr(Ports), ConsoleColor.Blue)
- Else
- W(String.Concat(">> Starting ", CStr(Ports), True, ConsoleColor.Blue)
- End If
- For i As Integer = StartPort To StartPort + Ports - 1
- Dim CurrentPort As Integer = i
- Dim T As New Thread(Sub() Prozessstarten(CurrentPort, Timeout))
- T.Name = "T" & CStr(CurrentPort)
- T.IsBackground = True
- T.Start()
- AllInstances.Add(T)
- Next
- Dim Wa As New Thread(Sub()
- While True
- If Not AllInstances.Any(Function(x) x.IsAlive()) Then
- Watch.Stop()
- W(String.Concat(Environment.NewLine, Environment.NewLine, " ", Ports, " instance(s) started in ", Watch.Elapsed.Seconds / 2, Watch.Elapsed.Milliseconds, " seconds."), False, ConsoleColor.DarkMagenta)
- Exit While
- End If
- End While
- End Sub)
- Wa.IsBackground = True
- Wa.Start()
- End Sub
- Private Sub Prozessstarten(Port As Integer, Timeout As Integer)
- Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyProgram"))
- Dim Pat As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyDict")
- Directory.CreateDirectory(Pat)
- Dim PSI As ProcessStartInfo = New ProcessStartInfo()
- PSI.FileName = "test.exe"
- PSI.Arguments = ....
- PSI.RedirectStandardOutput = True
- PSI.UseShellExecute = False
- PSI.WindowStyle = ProcessWindowStyle.Normal
- Dim Pro As Process = Process.Start(PSI)
- Dim Noww As DateTime = DateTime.Now
- While True
- Dim T As String = Pro.StandardOutput.ReadLine
- If DateTime.Now.Subtract(Noww).Milliseconds >= Timeout Then
- W(String.Concat(">> Error: Instance has a timeout."), False, ConsoleColor.Red)
- Exit While
- End If
- If T.Contains("DONE") Then
- W(">> started "), False, ConsoleColor.DarkCyan)
- Exit While
- ElseIf T.Contains("Could not start") Then
- W(">> Error: Could not start the instance.", False, ConsoleColor.Red)
- Exit While
- End If
- End While
- End Sub
Bietet sich da eine Möglichkeit, dies mit Threads aus dem Threadpool zu realisieren?
Achso: Timeouterkennung funktioniert übrigens auch nicht.
Wenn ja, wie?