Visual Basic (Visual Studio 2005) reindirizza inputstream al processo
-
05-07-2019 - |
Domanda
Sto cercando in rete da circa tre ore ora senza andare avanti. Non conosco VB molto bene, ma devo creare un programma wrapper per qualsiasi eseguibile che registra gli argomenti, tutte le informazioni di input, output ed err:
- Il mio wrapper si chiama: ad es. someApp.exe arg1 arg2
- Registra in someApp.log: arg1 arg2
- Chiama l'eseguibile originale: _someApp.exe arg1 arg2
- È necessario registrare e inoltrare qualsiasi input della console a _someApp process inputstream
- È necessario registrare qualsiasi flusso di output ed errore dal processo _someApp
Ok, ora sono bloccato al punto 4:
Dim p As New ProcessStartInfo
p.FileName = execute
p.Arguments = Command()
p.UseShellExecute = False
p.CreateNoWindow = True
p.RedirectStandardInput = True
p.RedirectStandardError = True
p.RedirectStandardOutput = True
Dim process As System.Diagnostics.Process
process = Diagnostics.Process.Start(p)
process.WaitForExit()
Dopo la fine di _someApp sono in grado di leggere ed errare il flusso per registrarlo, ma devo ancora fornire i miei wrapper al processo e voglio leggere ed errare il flusso quando succede.
Grazie per informazioni / esempi
Soluzione
Okay qui la soluzione ...
Variabili necessarie:
Private process As System.Diagnostics.Process
Private threadOut As Thread
Private streamOut As System.IO.StreamReader
Private threadErr As Thread
Private streamErr As System.IO.StreamReader
Private threadIn As Thread
Private streamIn As System.IO.StreamWriter
Sottotitoli necessari:
Private Sub ThreadTaskOut()
Dim line
While Not process.HasExited
line = streamOut.ReadToEnd
If line <> Nothing And line <> "" Then
log("Out: " & line)
Console.Out.Write(line)
End If
End While
End Sub
Private Sub ThreadTaskErr()
Dim line
While Not process.HasExited
line = streamErr.ReadToEnd
If line <> Nothing And line <> "" Then
log("Err: " & line)
Console.Error.Write(line)
End If
End While
End Sub
Private Sub ThreadTaskIn()
Dim line
While Not process.HasExited
line = Console.In.ReadLine
If line <> Nothing And line <> "" Then
log("In: " & line)
streamIn.WriteLine(line)
End If
End While
End Sub
Interno principale:
' create process information
Dim p As New ProcessStartInfo
p.FileName = execute
p.Arguments = Command()
p.UseShellExecute = False
p.CreateNoWindow = True
p.RedirectStandardInput = True
p.RedirectStandardError = True
p.RedirectStandardOutput = True
' log process start
log("Execute: " & execute & " " & Command())
' start process
process = Diagnostics.Process.Start(p)
' start thread for output stream
streamOut = process.StandardOutput
threadOut = New Thread(AddressOf ThreadTaskOut)
threadOut.IsBackground = True
threadOut.Start()
' start thread for error stream
streamErr = process.StandardError
threadErr = New Thread(AddressOf ThreadTaskErr)
threadErr.IsBackground = True
threadErr.Start()
' start thread for input stream
streamIn = process.StandardInput
threadIn = New Thread(AddressOf ThreadTaskIn)
threadIn.IsBackground = True
threadIn.Start()
' wait for the process to finish
process.WaitForExit()
log è un altro sub per accedere al file, esegui è la variabile che contiene _someApp.exe dal mio post iniziale. Non so ancora se la console del thread inputstream la oscura correttamente, perché la mia app spostata non ha input come sembra. Che qualcuno rilevi un errore ...
Per i miei scopi, hmm funziona come se ne avessi bisogno
Greetz, Ghad
Codice all'interno di main
Altri suggerimenti
Che ne dici di scrivere sull'app:
Dim sw as IO.StreamWriter = process.StandardInput
sw.WriteLine("Boo")
e per leggere dallo standard output:
Dim sr As IO.StreamReader = process.StandardOutput
Do
aString = sr.ReadLine()
Loop Until (sr.EndOfStream)